You are an expert Python programmer. Write correct, efficient Python 3 code that solves the problem and passes all tests. Output the code in the following format: ```python
<code>
```

PROBLEM:
There are an infinite amount of bags on a number line, one bag for each coordinate. Some of these bags contain coins.
You are given a 2D array coins, where coins[i] = [li, ri, ci] denotes that every bag from li to ri contains ci coins.
The segments that coins contain are non-overlapping.
You are also given an integer k.
Return the maximum amount of coins you can obtain by collecting k consecutive bags.

Example 1:

Input: coins = [[8,10,1],[1,3,2],[5,6,4]], k = 4
Output: 10
Explanation:
Selecting bags at positions [3, 4, 5, 6] gives the maximum number of coins: 2 + 0 + 4 + 4 = 10.

Example 2:

Input: coins = [[1,10,3]], k = 2
Output: 6
Explanation:
Selecting bags at positions [1, 2] gives the maximum number of coins: 3 + 3 = 6.

Constraints:

1 <= coins.length <= 105
1 <= k <= 109
coins[i] == [li, ri, ci]
1 <= li <= ri <= 109
1 <= ci <= 1000
The given segments are non-overlapping.

Your solution to the problem should be a method of the class Solution called maximumCoins and should pass all tests. Use the following starter code:
```python
class Solution:
    def maximumCoins(self, coins: List[List[int]], k: int) -> int:
        # Add your code here
```

SOLUTION:

Question:
PROBLEM:
There are an infinite amount of bags on a number line, one bag for each coordinate. Some of these bags contain coins.
You are given a 2D array coins, where coins[i] = [li, ri, ci] denotes that every bag from li to ri contains ci coins.
The segments that coins contain are non-overlapping.
You are also given an integer k.
Return the maximum amount of coins you can obtain by collecting k consecutive bags.

Example 1:

Input: coins = [[8,10,1],[1,3,2],[5,6,4]], k = 4
Output: 10
Explanation:
Selecting bags at positions [3, 4, 5, 6] gives the maximum number of coins: 2 + 0 + 4 + 4 = 10.

Example 2:

Input: coins = [[1,10,3]], k = 2
Output: 6
Explanation:
Selecting bags at positions [1, 2] gives the maximum number of coins: 3 + 3 = 6.

Constraints:

1 <= coins.length <= 105
1 <= k <= 109
coins[i] == [li, ri, ci]
1 <= li <= ri <= 109
1 <= ci <= 1000
The given segments are non-overlapping.

Your solution to the problem should be a method of the class Solution called maximumCoins and should pass all tests. Use the following starter code:
```python
class Solution:
    def maximumCoins(self, coins: List[List[int]], k: int) -> int:
        # Add your code here
```

SOLUTION:

assert Solution().maximumCoins(coins = [[1,10,3]], k = 2) == 6

Answer: assert Solution().maximumCoins(coins = [[8,10,1],[1,3,2],[5,6,4]], k = 4) == 10

Answer: assert Solution().maximumCoins(coins = [[1,10,3]], k = 2) == 6

Answer: assert Solution().maximumCoins(coins = [[10,20,5],[30,40,10],[50,60,15]], k = 5) == 75

Answer: assert Solution().maximumCoins(coins = [[1,2,5],[4,6,7],[8,10,3]], k = 3) == 21

Answer: assert Solution().maximumCoins(coins = [[1,1,1000],[2,2,1000],[3,3,1000]], k = 2) == 2000

Answer: assert Solution().maximumCoins(coins = [[5,5,10],[15,15,20],[25,25,30]], k = 1) == 30

Answer: assert Solution().maximumCoins(coins = [[2,3,1],[5,7,3],[9,11,2]], k = 5) == 11

Answer: assert Solution().maximumCoins(coins = [[1,2,1],[3,4,2],[5,6,3],[7,8,4]], k = 3) == 11

Answer: assert Solution().maximumCoins(coins = [[2,3,1],[5,7,3],[9,11,5]], k = 5) == 18

Answer: assert Solution().maximumCoins(coins = [[1,2,5],[4,6,2],[7,8,4]], k = 3) == 10

Answer: assert Solution().maximumCoins(coins = [[1,2,5],[4,6,7],[8,10,2]], k = 3) == 21

Answer: assert Solution().maximumCoins(coins = [[1,2,5],[4,5,7],[6,8,2]], k = 3) == 16

Answer: assert Solution().maximumCoins(coins = [[1,1,1],[2,2,1],[3,3,1],[4,4,1],[5,5,1],[6,6,1],[7,7,1],[8,8,1],[9,9,1],[10,10,1]], k = 10) == 10

Answer: assert Solution().maximumCoins(coins = [[1,5,10],[6,10,20],[11,15,15]], k = 5) == 100

Answer: assert Solution().maximumCoins(coins = [[1,1,10],[2,2,20],[3,3,30],[4,4,40]], k = 2) == 70

Answer: assert Solution().maximumCoins(coins = [[10,20,10],[30,40,20],[50,60,30]], k = 5) == 150

Answer: assert Solution().maximumCoins(coins = [[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5]], k = 2) == 9

Answer: assert Solution().maximumCoins(coins = [[1,1,100],[2,2,200],[3,3,300]], k = 2) == 500

Answer: assert Solution().maximumCoins(coins = [[1,2,100],[100,200,200],[300,400,300],[500,600,400],[700,800,500],[900,1000,600]], k = 150) == 60600

Answer: assert Solution().maximumCoins(coins = [[1,10,10],[11,20,20],[21,30,30],[31,40,40],[41,50,50]], k = 35) == 1300

Answer: assert Solution().maximumCoins(coins = [[10,19,5],[25,34,10],[40,49,15],[55,64,20],[70,79,25],[85,94,30]], k = 25) == 550

Answer: assert Solution().maximumCoins(coins = [[1,10,1],[11,20,2],[21,30,3],[31,40,4],[41,50,5],[51,60,6],[61,70,7],[71,80,8],[81,90,9],[91,100,10]], k = 10) == 100

Answer: assert Solution().maximumCoins(coins = [[1,10,5],[12,20,10],[22,30,15],[32,40,20],[42,50,25]], k = 10) == 225

Answer: assert Solution().maximumCoins(coins = [[1,3,200],[5,7,300],[9,12,400],[14,17,500],[19,22,600]], k = 5) == 2400

Answer: assert Solution().maximumCoins(coins = [[1,5,5],[10,15,10],[20,25,15],[30,35,20],[40,45,25],[50,55,30],[60,65,35],[70,75,40]], k = 30) == 630

Answer: assert Solution().maximumCoins(coins = [[1,2,100],[3,6,200],[7,10,150],[11,14,100],[15,18,50]], k = 7) == 1250

Answer: assert Solution().maximumCoins(coins = [[5,10,1],[15,20,2],[25,30,3],[35,40,4],[45,50,5],[55,60,6],[65,70,7],[75,80,8]], k = 15) == 83

Answer: assert Solution().maximumCoins(coins = [[1,3,2],[5,7,3],[10,12,5],[15,17,4],[20,22,6]], k = 5) == 18

Answer: assert Solution().maximumCoins(coins = [[1,10,10],[11,20,20],[21,30,30],[31,40,40],[41,50,50],[51,60,60],[61,70,70]], k = 30) == 1800

Answer: assert Solution().maximumCoins(coins = [[1,2,1],[3,4,2],[5,6,3],[7,8,4],[9,10,5],[11,12,6],[13,14,7],[15,16,8],[17,18,9],[19,20,10],[21,22,11]], k = 12) == 102

Answer: assert Solution().maximumCoins(coins = [[1,10,1],[12,21,2],[23,32,3],[34,43,4],[45,54,5]], k = 15) == 66

Answer: assert Solution().maximumCoins(coins = [[1,2,100],[3,4,200],[5,6,300],[7,8,400],[9,10,500],[11,12,600],[13,14,700],[15,16,800],[17,18,900],[19,20,1000]], k = 10) == 8000

Answer: assert Solution().maximumCoins(coins = [[1,5,1000],[6,10,900],[11,15,800],[16,20,700],[21,25,600],[26,30,500],[31,35,400],[36,40,300]], k = 25) == 20000

Answer: assert Solution().maximumCoins(coins = [[1,500000000,1],[500000001,1000000000,2],[1000000001,1500000000,3],[1500000001,2000000000,4],[2000000001,2500000000,5]], k = 100000000) == 500000000

Answer: assert Solution().maximumCoins(coins = [[1,3,100],[6,9,50],[11,15,200],[20,22,300],[25,30,150]], k = 6) == 1050

Answer: assert Solution().maximumCoins(coins = [[1,3,100],[5,7,200],[9,11,300],[13,15,400],[17,19,500],[21,23,600],[25,27,700],[29,31,800],[33,35,900]], k = 5) == 3500

Answer: assert Solution().maximumCoins(coins = [[1,1,1],[2,3,2],[4,5,3],[6,7,4],[8,9,5],[10,11,6],[12,13,7],[14,15,8],[16,17,9],[18,19,10]], k = 5) == 46

Answer: assert Solution().maximumCoins(coins = [[1,2,50],[5,6,75],[10,12,25],[15,18,100],[20,23,50],[25,27,25],[30,35,100]], k = 10) == 650

Answer: assert Solution().maximumCoins(coins = [[1,2,1000],[4,6,900],[8,10,800],[12,14,700],[16,18,600]], k = 6) == 4700

Answer: assert Solution().maximumCoins(coins = [[1,2,100],[4,5,150],[7,8,200],[10,11,250],[13,14,300]], k = 4) == 850

Answer: assert Solution().maximumCoins(coins = [[1,10,10],[20,30,20],[40,50,30],[60,70,40],[80,90,50]], k = 20) == 550

Answer: assert Solution().maximumCoins(coins = [[1,100,5],[200,300,3],[400,500,2],[600,700,8],[800,900,6]], k = 100) == 800

Answer: assert Solution().maximumCoins(coins = [[1,3,10],[4,6,20],[7,9,30],[10,12,40],[13,15,50],[16,18,60],[19,21,70],[22,24,80],[25,27,90]], k = 9) == 720

Answer: assert Solution().maximumCoins(coins = [[1,3,2],[5,7,3],[10,12,4],[15,17,5],[20,22,6]], k = 5) == 18

Answer: assert Solution().maximumCoins(coins = [[1,3,50],[5,7,60],[9,11,70],[13,15,80],[17,19,90],[21,23,100],[25,27,110]], k = 9) == 720

Answer: assert Solution().maximumCoins(coins = [[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5],[6,6,6],[7,7,7],[8,8,8],[9,9,9],[10,10,10],[11,11,11],[12,12,12],[13,13,13],[14,14,14],[15,15,15]], k = 7) == 84

Answer: assert Solution().maximumCoins(coins = [[1,100,1],[101,200,2],[201,300,3],[301,400,4],[401,500,5]], k = 50) == 250

Answer: assert Solution().maximumCoins(coins = [[1,3,10],[5,7,15],[9,11,20],[13,15,25],[17,19,30],[21,23,35],[25,27,40]], k = 7) == 225

Answer: assert Solution().maximumCoins(coins = [[1,2,100],[3,4,200],[5,6,300],[7,8,400],[9,10,500],[11,12,600],[13,14,700]], k = 5) == 3100

Answer: assert Solution().maximumCoins(coins = [[1,4,30],[5,8,25],[9,12,20],[13,16,15],[17,20,10],[21,24,5],[25,28,2],[29,32,1]], k = 15) == 345

Answer: assert Solution().maximumCoins(coins = [[1,5,5],[10,15,10],[20,25,15],[30,35,20],[40,45,25]], k = 10) == 150

Answer: assert Solution().maximumCoins(coins = [[1,5,1],[6,10,2],[11,15,3],[16,20,4],[21,25,5],[26,30,6],[31,35,7]], k = 15) == 90

Answer: assert Solution().maximumCoins(coins = [[1,1,1000],[2,2,999],[3,3,998],[4,4,997],[5,5,996],[6,6,995],[7,7,994],[8,8,993]], k = 4) == 3994

Answer: assert Solution().maximumCoins(coins = [[1,3,10],[5,7,20],[10,15,5],[20,25,15]], k = 5) == 75

Answer: assert Solution().maximumCoins(coins = [[1,1,1],[2,2,1],[3,3,1],[4,4,1],[5,5,1],[6,6,1],[7,7,1],[8,8,1],[9,9,1],[10,10,1],[11,11,1],[12,12,1],[13,13,1],[14,14,1],[15,15,1],[16,16,1],[17,17,1],[18,18,1],[19,19,1],[20,20,1]], k = 10) == 10

Answer: assert Solution().maximumCoins(coins = [[1,1,100],[3,5,200],[7,10,300],[12,15,400],[18,20,500]], k = 5) == 1600

Answer: assert Solution().maximumCoins(coins = [[1,1,100],[3,3,200],[5,5,300],[7,7,400],[9,9,500]], k = 3) == 900

Answer: assert Solution().maximumCoins(coins = [[1,2,1],[3,5,2],[6,7,3],[8,10,4],[11,12,5],[13,15,6],[16,17,7],[18,20,8],[21,22,9],[23,25,10]], k = 7) == 64

Answer: assert Solution().maximumCoins(coins = [[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5],[6,6,6],[7,7,7],[8,8,8],[9,9,9],[10,10,10]], k = 4) == 34

Answer: assert Solution().maximumCoins(coins = [[1,100,5],[200,300,10],[400,500,15],[600,700,20],[800,900,25]], k = 50) == 1250

Answer: assert Solution().maximumCoins(coins = [[1,5,50],[10,20,75],[30,40,25],[50,60,100],[70,80,75],[90,100,50],[110,120,25],[130,140,125]], k = 40) == 1925

Answer: assert Solution().maximumCoins(coins = [[1,5,10],[6,10,5],[11,15,8],[16,20,12]], k = 6) == 68

Answer: assert Solution().maximumCoins(coins = [[1,10,100],[20,30,200],[40,50,300],[60,70,400],[80,90,500]], k = 25) == 7500

Answer: assert Solution().maximumCoins(coins = [[1,1,1],[2,2,1],[3,3,1],[4,4,1],[5,5,1],[6,6,1],[7,7,1],[8,8,1],[9,9,1],[10,10,1],[11,11,1],[12,12,1],[13,13,1],[14,14,1],[15,15,1],[16,16,1],[17,17,1],[18,18,1],[19,19,1],[20,20,1]], k = 20) == 20

Answer: assert Solution().maximumCoins(coins = [[1,2,1],[3,4,2],[5,6,3],[7,8,4],[9,10,5],[11,12,6],[13,14,7],[15,16,8],[17,18,9],[19,20,10]], k = 5) == 46

Answer: assert Solution().maximumCoins(coins = [[1,1000,1],[2000,3000,2],[4000,5000,3],[6000,7000,4],[8000,9000,5]], k = 1000) == 5000

Answer: assert Solution().maximumCoins(coins = [[1,5,50],[6,10,100],[11,15,150],[16,20,200],[21,25,250],[26,30,300]], k = 12) == 3150

Answer: assert Solution().maximumCoins(coins = [[1,10,10],[20,30,20],[40,50,30],[60,70,40],[80,90,50],[100,110,60],[120,130,70],[140,150,80],[160,170,90],[180,190,100]], k = 20) == 1100

Answer: assert Solution().maximumCoins(coins = [[1,10,1],[20,30,2],[40,50,3],[60,70,4],[80,90,5]], k = 15) == 55

Answer: assert Solution().maximumCoins(coins = [[1,2,500],[3,4,400],[5,6,300],[7,8,200],[9,10,100]], k = 5) == 2100

Answer: assert Solution().maximumCoins(coins = [[1,3,5],[5,8,7],[10,12,3],[15,20,9],[25,30,4],[35,40,8],[45,50,6]], k = 10) == 60

Answer: assert Solution().maximumCoins(coins = [[1,3,100],[10,20,50],[30,40,25],[50,60,125],[70,80,75]], k = 15) == 1375

Answer: assert Solution().maximumCoins(coins = [[100,105,100],[106,110,150],[111,120,200],[121,130,250],[131,140,300]], k = 20) == 5500

Answer: assert Solution().maximumCoins(coins = [[1,5,10],[10,15,20],[20,25,30],[30,35,40],[40,45,50],[50,55,60],[60,65,70],[70,75,80]], k = 25) == 1200

Answer: assert Solution().maximumCoins(coins = [[1,1,500],[2,2,400],[3,3,300],[4,4,200],[5,5,100]], k = 5) == 1500

Answer: assert Solution().maximumCoins(coins = [[1,3,2],[5,7,3],[9,11,1],[13,15,4],[17,19,2],[21,23,3]], k = 5) == 14

Answer: assert Solution().maximumCoins(coins = [[1,2,1],[3,4,2],[5,6,3],[7,8,4],[9,10,5],[11,12,6],[13,14,7],[15,16,8],[17,18,9],[19,20,10]], k = 10) == 80

Answer: assert Solution().maximumCoins(coins = [[1,2,10],[4,6,20],[8,10,30],[12,14,40],[16,18,50]], k = 6) == 230

Answer: assert Solution().maximumCoins(coins = [[1,1,1],[10,10,2],[20,20,3],[30,30,4],[40,40,5],[50,50,6],[60,60,7],[70,70,8],[80,80,9],[90,90,10]], k = 5) == 10

Answer: assert Solution().maximumCoins(coins = [[1,5,50],[10,15,100],[20,25,150],[30,35,200],[40,45,250],[50,55,300]], k = 30) == 4500

Answer: assert Solution().maximumCoins(coins = [[1,4,50],[6,9,75],[11,14,100],[16,19,125],[21,24,150]], k = 3) == 450

Answer: assert Solution().maximumCoins(coins = [[1,3,10],[5,7,15],[9,11,5],[13,15,20]], k = 4) == 60

Answer: assert Solution().maximumCoins(coins = [[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5],[6,6,6],[7,7,7],[8,8,8],[9,9,9],[10,10,10],[11,11,11],[12,12,12],[13,13,13],[14,14,14],[15,15,15],[16,16,16],[17,17,17],[18,18,18],[19,19,19],[20,20,20]], k = 15) == 195

Answer: assert Solution().maximumCoins(coins = [[1,1000000,1],[1000001,2000000,2],[2000001,3000000,3]], k = 1000000) == 3000000

Answer: assert Solution().maximumCoins(coins = [[1,2,100],[4,5,200],[7,8,300],[10,11,400],[13,14,500],[16,17,600],[19,20,700]], k = 7) == 3100

Answer: assert Solution().maximumCoins(coins = [[1,1,1],[2,2,1],[3,3,1],[4,4,1],[5,5,1],[6,6,1],[7,7,1],[8,8,1],[9,9,1],[10,10,1],[11,11,1],[12,12,1],[13,13,1],[14,14,1],[15,15,1],[16,16,1],[17,17,1],[18,18,1],[19,19,1],[20,20,1]], k = 5) == 5

Answer: assert Solution().maximumCoins(coins = [[1,2,1],[2,3,2],[3,4,3],[4,5,4],[5,6,5],[6,7,6],[7,8,7],[8,9,8],[9,10,9],[10,11,10],[11,12,11]], k = 5) == 83

Answer: assert Solution().maximumCoins(coins = [[1,3,100],[6,8,200],[10,12,150],[14,16,50],[18,20,300]], k = 3) == 900

Answer: assert Solution().maximumCoins(coins = [[1,1,1000],[2,2,1000],[3,3,1000],[4,4,1000],[5,5,1000],[6,6,1000],[7,7,1000],[8,8,1000],[9,9,1000],[10,10,1000]], k = 3) == 3000

Answer: assert Solution().maximumCoins(coins = [[1,2,10],[4,5,20],[7,8,30],[10,11,40],[13,14,50],[16,17,60],[19,20,70]], k = 7) == 310

Answer: assert Solution().maximumCoins(coins = [[1,5,5],[6,10,15],[11,15,25],[16,20,35],[21,25,45],[26,30,55]], k = 10) == 500

Answer: assert Solution().maximumCoins(coins = [[1,2,5],[3,4,10],[5,6,15],[7,8,20],[9,10,25],[11,12,30],[13,14,35],[15,16,40]], k = 4) == 150

Answer: assert Solution().maximumCoins(coins = [[1,10,1],[11,20,2],[21,30,3],[31,40,4],[41,50,5],[51,60,6],[61,70,7],[71,80,8],[81,90,9],[91,100,10]], k = 30) == 270

Answer: assert Solution().maximumCoins(coins = [[1,2,500],[4,5,400],[7,8,300],[10,11,200],[13,14,100]], k = 3) == 1000

Answer: assert Solution().maximumCoins(coins = [[1,10,5],[15,25,7],[30,40,3],[45,55,8],[60,70,2]], k = 15) == 88

Answer: assert Solution().maximumCoins(coins = [[1,5,10],[6,10,20],[11,15,30],[16,20,40],[21,25,50]], k = 10) == 450

Answer: assert Solution().maximumCoins(coins = [[1,5,1],[7,10,2],[12,15,3],[18,20,4],[22,25,5]], k = 5) == 20

Answer: assert Solution().maximumCoins(coins = [[1,10,10],[20,25,20],[30,35,30],[40,45,40],[50,55,50],[60,65,60],[70,75,70]], k = 15) == 720

Answer: assert Solution().maximumCoins(coins = [[1,1,100],[10,19,90],[20,29,80],[30,39,70],[40,49,60],[50,59,50],[60,69,40],[70,79,30],[80,89,20]], k = 20) == 1700

Answer: assert Solution().maximumCoins(coins = [[1,3,10],[6,8,15],[12,15,20],[20,25,25]], k = 7) == 150

Answer: assert Solution().maximumCoins(coins = [[1,2,500],[4,6,300],[8,10,200],[12,14,100],[16,18,400]], k = 6) == 1900

Answer: assert Solution().maximumCoins(coins = [[1,1,1],[2,2,1],[3,3,1],[4,4,1],[5,5,1],[6,6,1],[7,7,1],[8,8,1],[9,9,1],[10,10,1],[11,11,1],[12,12,1],[13,13,1],[14,14,1],[15,15,1]], k = 10) == 10

Answer: assert Solution().maximumCoins(coins = [[1,5,1],[6,10,2],[11,15,3],[16,20,4],[21,25,5],[26,30,6],[31,35,7],[36,40,8],[41,45,9]], k = 10) == 85

Answer: assert Solution().maximumCoins(coins = [[1,2,50],[4,6,30],[8,10,20],[12,14,10],[16,18,5]], k = 4) == 130

Answer: assert Solution().maximumCoins(coins = [[1,1,100],[3,3,200],[5,5,300],[7,7,400],[9,9,500],[11,11,600],[13,13,700],[15,15,800]], k = 8) == 2600

Answer: assert Solution().maximumCoins(coins = [[1,1,100],[2,2,200],[3,3,300],[4,4,400],[5,5,500],[6,6,600],[7,7,700],[8,8,800],[9,9,900],[10,10,1000],[11,11,1100],[12,12,1200],[13,13,1300],[14,14,1400]], k = 7) == 7700

Answer: assert Solution().maximumCoins(coins = [[1,2,1],[3,4,1],[5,6,1],[7,8,1],[9,10,1],[11,12,1],[13,14,1],[15,16,1],[17,18,1],[19,20,1]], k = 15) == 15

Answer: assert Solution().maximumCoins(coins = [[1,3,10],[5,7,20],[10,12,30],[15,17,40],[20,22,50]], k = 5) == 150

Answer: assert Solution().maximumCoins(coins = [[1,2,3],[4,6,7],[8,10,9],[12,14,11],[16,18,13],[20,22,15],[24,26,17]], k = 6) == 81

Answer: assert Solution().maximumCoins(coins = [[1,5,50],[6,10,100],[11,15,150],[16,20,200],[21,25,250]], k = 6) == 1450

Answer: assert Solution().maximumCoins(coins = [[1,5,10],[10,15,5],[20,25,7],[30,35,8]], k = 10) == 55

Answer: assert Solution().maximumCoins(coins = [[1,10,100],[15,25,200],[30,40,150],[45,55,50]], k = 10) == 2000

Answer: assert Solution().maximumCoins(coins = [[1,1,5],[2,2,5],[3,3,5],[4,4,5],[5,5,5],[6,6,5],[7,7,5],[8,8,5],[9,9,5],[10,10,5],[11,11,5],[12,12,5]], k = 8) == 40

Answer: assert Solution().maximumCoins(coins = [[1,5,10],[6,10,20],[11,15,30],[16,20,40],[21,25,50],[26,30,60],[31,35,70],[36,40,80],[41,45,90]], k = 15) == 1200

Answer: assert Solution().maximumCoins(coins = [[1,2,10],[4,6,20],[7,10,30],[11,15,40],[16,20,50],[21,25,60]], k = 12) == 630

import random
import functools
import collections
import string
import math
import datetime

from typing import *
from functools import *
from collections import *
from itertools import *
from heapq import *
from bisect import *
from string import *
from operator import *
from math import *

inf = float('inf')

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def list_node(values: list):
    if not values:
        return None
    head = ListNode(values[0])
    p = head
    for val in values[1:]:
        node = ListNode(val)
        p.next = node
        p = node
    return head

def is_same_list(p1, p2):
    if p1 is None and p2 is None:
        return True
    if not p1 or not p2:
        return False
    return p1.val == p2.val and is_same_list(p1.next, p2.next)

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def tree_node(values: list):
    if not values:
        return None
    root = TreeNode(values[0])
    i = 1
    queue = deque()
    queue.append(root)
    while queue:
        node = queue.popleft()
        if i < len(values) and values[i] is not None:
            node.left = TreeNode(values[i])
            queue.append(node.left)
        i += 1
        if i < len(values) and values[i] is not None:
            node.right = TreeNode(values[i])
            queue.append(node.right)
        i += 1
    return root

def is_same_tree(p, q):
    if not p and not q:
        return True
    elif not p or not q:
        return False
    elif p.val != q.val:
        return False
    else:
        return is_same_tree(p.left, q.left) and is_same_tree(p.right, q.right)

class Solution:
  def maximumCoins(self, coins: list[list[int]], k: int) -> int:
    return max(self._slide(coins, k),
               self._slide([[-r, -l, c] for l, r, c in coins], k))

  def _slide(self, coins: list[list[int]], k: int) -> int:
    coins.sort()
    res = 0
    windowSum = 0
    j = 0
    for li, ri, ci in coins:  # Consider the number line [li..li + k).
      rightBoundary = li + k

      # [lj, rj] is fully in [li..li + k).
      while j + 1 < len(coins) and coins[j + 1][0] < rightBoundary:
        lj, rj, cj = coins[j]
        windowSum += (rj - lj + 1) * cj
        j += 1

      # [lj, rj] may be partially in [l..l + k).
      last = 0
      if j < len(coins) and coins[j][0] < rightBoundary:
        lj, rj, cj = coins[j]
        last = (min(rightBoundary - 1, rj) - lj + 1) * cj

      res = max(res, windowSum + last)
      windowSum -= (ri - li + 1) * ci
    return res


You are an expert Python programmer. Write correct, efficient Python 3 code that solves the problem and passes all tests. Output the code in the following format: ```python
<code>
```

PROBLEM:
Given an integer array nums, find the number of subsequences of size 5 of nums with a unique middle mode.
Since the answer may be very large, return it modulo 109 + 7.
A mode of a sequence of numbers is defined as the element that appears the maximum number of times in the sequence.
A sequence of numbers contains a unique mode if it has only one mode.
A sequence of numbers seq of size 5 contains a unique middle mode if the middle element (seq[2]) is a unique mode.

Example 1:
Input: nums = [1,1,1,1,1,1]
Output: 6
Explanation:
[1, 1, 1, 1, 1] is the only subsequence of size 5 that can be formed from this list, and it has a unique middle mode of 1.
Example 2:
Input: nums = [1,2,2,3,3,4]
Output: 4
Explanation:
[1, 2, 2, 3, 4] and [1, 2, 3, 3, 4] have unique middle modes because the number at index 2 has the greatest frequency in the subsequence. [1, 2, 2, 3, 3] does not have a unique middle mode because 2 and 3 both appear twice in the subsequence.
Example 3:
Input: nums = [0,1,2,3,4,5,6,7,8]
Output: 0
Explanation:
There does not exist a subsequence of length 5 with a unique middle mode.

Constraints:

5 <= nums.length <= 105
-109 <= nums[i] <= 109

Your solution to the problem should be a method of the class Solution called subsequencesWithMiddleMode and should pass all tests. Use the following starter code:
```python
class Solution:
    def subsequencesWithMiddleMode(self, nums: List[int]) -> int:
        # Add your code here
```

SOLUTION:

Question:
PROBLEM:
Given an integer array nums, find the number of subsequences of size 5 of nums with a unique middle mode.
Since the answer may be very large, return it modulo 109 + 7.
A mode of a sequence of numbers is defined as the element that appears the maximum number of times in the sequence.
A sequence of numbers contains a unique mode if it has only one mode.
A sequence of numbers seq of size 5 contains a unique middle mode if the middle element (seq[2]) is a unique mode.

Example 1:
Input: nums = [1,1,1,1,1,1]
Output: 6
Explanation:
[1, 1, 1, 1, 1] is the only subsequence of size 5 that can be formed from this list, and it has a unique middle mode of 1.
Example 2:
Input: nums = [1,2,2,3,3,4]
Output: 4
Explanation:
[1, 2, 2, 3, 4] and [1, 2, 3, 3, 4] have unique middle modes because the number at index 2 has the greatest frequency in the subsequence. [1, 2, 2, 3, 3] does not have a unique middle mode because 2 and 3 both appear twice in the subsequence.
Example 3:
Input: nums = [0,1,2,3,4,5,6,7,8]
Output: 0
Explanation:
There does not exist a subsequence of length 5 with a unique middle mode.

Constraints:

5 <= nums.length <= 105
-109 <= nums[i] <= 109

Your solution to the problem should be a method of the class Solution called subsequencesWithMiddleMode and should pass all tests. Use the following starter code:
```python
class Solution:
    def subsequencesWithMiddleMode(self, nums: List[int]) -> int:
        # Add your code here
```

SOLUTION:

assert Solution().subsequencesWithMiddleMode(nums = [1,1,2,2,2,3,3,3,3,4,4,4,4,4]) == 951

assert Solution().subsequencesWithMiddleMode(nums = [10,9,8,7,6,5,4,3,2,1]) == 0

assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5,5,5,5,5,5,5,5,5,5,5]) == 2772

assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,3,3,4]) == 4

assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5,6,7,8,9,10]) == 0

assert Solution().subsequencesWithMiddleMode(nums = [10,20,20,30,30,30,40,40,40,40]) == 123

assert Solution().subsequencesWithMiddleMode(nums = [0,1,2,3,4,5,6,7,8]) == 0

assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,3,3,4,4,4]) == 22

assert Solution().subsequencesWithMiddleMode(nums = [1,1,2,2,3,3,4,4,5,5]) == 80

assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5]) == 0

assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5,1,2,3,4,5]) == 50

assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,2,2,2,3,3,3,4,4,4,5,5,5]) == 1140

assert Solution().subsequencesWithMiddleMode(nums = [5,5,5,5,5,5,5,5,5,5]) == 252

assert Solution().subsequencesWithMiddleMode(nums = [10,20,10,20,10,20,10,20,10,20]) == 162

assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,1,1]) == 6

assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]) == 1409

assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,2,3,3,3,3,4,4,4,4,4,5,5]) == 1658

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,2,2,2,3,3,3,3,4,4,4,4,4]) == 951

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,2,2,2,3,3,4,4,4,4,5,5,5,5,5]) == 1822

Answer: assert Solution().subsequencesWithMiddleMode(nums = [10,9,8,7,6,5,4,3,2,1]) == 0

Answer: assert Solution().subsequencesWithMiddleMode(nums = [-1,-2,-3,-4,-5,-1,-2,-3,-4,-5,-1,-2,-3,-4,-5]) == 760

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5,5,5,5,5,5,5,5,5,5,5]) == 2772

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,3,3,4]) == 4

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5,6,7,8,9,10]) == 0

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1000000000,1000000000,1000000000,1000000000,1000000000,1,2,3,4,5]) == 126

Answer: assert Solution().subsequencesWithMiddleMode(nums = [10,20,20,30,30,30,40,40,40,40]) == 123

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5]) == 4260

Answer: assert Solution().subsequencesWithMiddleMode(nums = [0,1,2,3,4,5,6,7,8]) == 0

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,3,3,4,4,4]) == 22

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,2,2,3,3,4,4,5,5]) == 80

Answer: assert Solution().subsequencesWithMiddleMode(nums = [5,4,3,2,1,5,4,3,2,1,5,4,3,2,1,5,4,3,2,1]) == 4260

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,5]) == 1614

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5]) == 0

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5,1,2,3,4,5]) == 50

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1000000000,1000000000,1000000000,1000000000,1000000000,2,3,4,5]) == 81

Answer: assert Solution().subsequencesWithMiddleMode(nums = [-1,-2,-2,-3,-3,-3,-4,-4,-4,-4,-5,-5,-5,-5,-5]) == 1409

Answer: assert Solution().subsequencesWithMiddleMode(nums = [10,20,10,20,10,20,10,20,10,20,10,20,10,20,10]) == 1953

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9]) == 22896

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,2,2,2,3,3,3,4,4,4,5,5,5]) == 1140

Answer: assert Solution().subsequencesWithMiddleMode(nums = [5,5,5,5,5,5,5,5,5,5]) == 252

Answer: assert Solution().subsequencesWithMiddleMode(nums = [10,20,10,20,10,20,10,20,10,20]) == 162

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,1,1]) == 6

Answer: assert Solution().subsequencesWithMiddleMode(nums = [10,20,20,30,30,30,40,40,40,40,50,50,50,50,50]) == 1409

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]) == 0

Answer: assert Solution().subsequencesWithMiddleMode(nums = [-1000000000,-1000000000,-1000000000,-999999999,-999999999,-999999998,-999999997,-999999996,-999999995]) == 33

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1000000000,1000000000,1000000000,1000000000,1,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4]) == 106632

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5]) == 1655486

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9,10,10,10,11,11,11,12,12,12,13,13,13,14,14,14,15,15,15]) == 192738

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5]) == 122481

Answer: assert Solution().subsequencesWithMiddleMode(nums = [5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5]) == 658008

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,4,4,5,5,5,6,6,7,7,8,8,9,9,10,10,10,10,10]) == 60701

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9,10,10,10]) == 103847

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9,10,10,10,11,11,11,12,12,12,13,13,13,14,14,14,15,15,15,16,16,16,17,17,17,18,18,18,19,19,19,20,20,20]) == 708453

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5]) == 119267

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,2,3,3,3,4,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9,9]) == 24840

Answer: assert Solution().subsequencesWithMiddleMode(nums = [-1,-1,-1,-1,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11]) == 16088

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2]) == 169911

Answer: assert Solution().subsequencesWithMiddleMode(nums = [5,5,5,5,5,5,5,5,5,5,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2]) == 81756

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5]) == 64883

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]) == 1498694

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,2,3,3,3,3,3,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5]) == 1023864

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,1,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5]) == 137108

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7]) == 91461

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]) == 0

Answer: assert Solution().subsequencesWithMiddleMode(nums = [0,0,0,0,0,1,1,1,1,2,2,2,3,3,4,4,5,5,5,6,6,6,6,6,7,7,7,7,7,7]) == 41124

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]) == 8936928

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,4,5,5,5,6,6,6,6,7,7,7,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9]) == 69216

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5]) == 34819

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5]) == 6320

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,2,2,2,3,3,3,4,4,4,4,5,5,5,5,5,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7]) == 82343

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8,9,9,9,9,9,10,10,10,10,10]) == 224464

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,2,2,2,3,3,4,4,4,4,5,5,6,6,7,7,7]) == 5104

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,1,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5,5,5]) == 10564

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,2,3,3,4,4,5,5,5,6,6,7,7,8,8,9,9,10,10,10,11,11,12,12]) == 14345

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7,7,7]) == 45186

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14]) == 16448

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,2,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,10,10,10,10,10,10]) == 27828

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5,5,5,5,5,5,5,6,7,8,9,10,10,10,10,10,10,10,10,10,10]) == 27636

Answer: assert Solution().subsequencesWithMiddleMode(nums = [5,5,5,5,5,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5]) == 96507

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10]) == 3360

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5]) == 81138

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,2,2,2,3,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15]) == 43016

Answer: assert Solution().subsequencesWithMiddleMode(nums = [7,7,7,7,8,8,8,9,9,10,10,11,12,13,14]) == 937

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,1,1,1,1,1,1,1,1,1]) == 27252

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5,5,5,5,6,6,6,6,6,6,6]) == 29636

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11,12,12,12,12]) == 426096

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,19]) == 62016

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,2,2,2,2,3,3,3,3,3,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5]) == 47112

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11]) == 288200

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5,5,5,5,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9]) == 3728818

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,2,2,2,3,3,3,3,4,4,4,5,5,5,6,6,6,7,7,8,8,9,9,10,10]) == 19578

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5,6,7,8,9,10,10,10,10,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]) == 28506

Answer: assert Solution().subsequencesWithMiddleMode(nums = [-1,-1,-1,-1,0,0,0,1,1,1,1,2,2,2,2,2,3,3,3,3,4,4,4,5,5,6,6,7,7,8,8,9,9]) == 71388

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]) == 0

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,1,2,2,2,2,3,3,3,4,4,5,5,6,7,8,9,10,11,12,13,14,15]) == 13262

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5,5,5,5,5,6,6,6,6,6,6,6,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8]) == 81743

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8,9,9,9,9,9]) == 119303

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]) == 1409

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]) == 789008

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,2,2,2,3,3,4,4,4,4,5,5,6,6,6,6,6]) == 5314

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]) == 2748991

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,20,20,20,20]) == 1806

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]) == 0

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7]) == 68479

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,2,3,3,3,3,4,4,4,4,4,5,5]) == 1658

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,10,10,10,10,10,10,10,10]) == 31540

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]) == 2349060

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]) == 1868754

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,1,1,2,2,2,2,3,3,3,4,4,5,6,7,8,9]) == 5648

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,1,2,2,2,2,3,3,3,4,4,5,5,6,7,8,9,10,10,10,10,10]) == 12854

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 5006386

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10]) == 133020

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,2,2,2,3,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]) == 4395

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,19,20,20]) == 77520

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,2,2,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9]) == 40929

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,1,2,2,2,2,2,3,3,3,4,4,4,5,5,5,5,6,6,6,6,6,7,7,7,7,7,7]) == 56206

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 142506

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,1,1,2,2,2,2,2,3,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10]) == 26620

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3]) == 81756

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9]) == 114912

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50]) == 0

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,4,4,5,5,5,6,6,7,7,8,8,9,9,10,10,10,10,10,10,10,10,10,10,1,1,1,1,1,1,1,1,1,1]) == 415949

Answer: assert Solution().subsequencesWithMiddleMode(nums = [10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250]) == 0

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5,5,5,6,7,8,9,10,10,10,10,11,12,13,14,15,15,15,15,15]) == 9066

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,1,2,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,10,10,10,10,10,10,10,10]) == 54004

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5]) == 169880

Answer: assert Solution().subsequencesWithMiddleMode(nums = [-1000000000, -999999999, -999999998, -999999997, -999999996, -999999995, 0, 1, 2, 3, 4, 5, 5, 5, 5, 5, 6, 7, 8, 9, 10]) == 5131

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9,10,10,10,11,11,11,12,12,12]) == 86526

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10]) == 186840

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5]) == 159797

Answer: assert Solution().subsequencesWithMiddleMode(nums = [-1,-1,-1,-1,-2,-2,-2,-3,-3,-4,-4,-5,-6,-7,-8,-9,-10,-11,-12,-13]) == 2612

Answer: assert Solution().subsequencesWithMiddleMode(nums = [7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7]) == 201376

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1000000000,1000000000,1000000000,999999999,999999999,999999999,999999998,999999998,999999997,999999997]) == 96

Answer: assert Solution().subsequencesWithMiddleMode(nums = [-1,-1,-1,-1,-1,0,0,0,1,1,1,2,2,3,3,4,4,5,5,5,6,6,7,7,8,8,9,9,10,10]) == 29112

Answer: assert Solution().subsequencesWithMiddleMode(nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,1,1,1,1,1]) == 3126

Answer: assert Solution().subsequencesWithMiddleMode(nums = [5,5,5,6,6,6,7,7,7,8,8,8,9,9,9,10,10,10,11,11,11]) == 6741

Answer: assert Solution().subsequencesWithMiddleMode(nums = [-1,-1,-1,0,0,0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8]) == 37530

import random
import functools
import collections
import string
import math
import datetime

from typing import *
from functools import *
from collections import *
from itertools import *
from heapq import *
from bisect import *
from string import *
from operator import *
from math import *

inf = float('inf')

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def list_node(values: list):
    if not values:
        return None
    head = ListNode(values[0])
    p = head
    for val in values[1:]:
        node = ListNode(val)
        p.next = node
        p = node
    return head

def is_same_list(p1, p2):
    if p1 is None and p2 is None:
        return True
    if not p1 or not p2:
        return False
    return p1.val == p2.val and is_same_list(p1.next, p2.next)

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def tree_node(values: list):
    if not values:
        return None
    root = TreeNode(values[0])
    i = 1
    queue = deque()
    queue.append(root)
    while queue:
        node = queue.popleft()
        if i < len(values) and values[i] is not None:
            node.left = TreeNode(values[i])
            queue.append(node.left)
        i += 1
        if i < len(values) and values[i] is not None:
            node.right = TreeNode(values[i])
            queue.append(node.right)
        i += 1
    return root

def is_same_tree(p, q):
    if not p and not q:
        return True
    elif not p or not q:
        return False
    elif p.val != q.val:
        return False
    else:
        return is_same_tree(p.left, q.left) and is_same_tree(p.right, q.right)

# Recall from solution 1 that after counting all the subsequences with `a` as
# the middle mode number, we need to subtract the cases where `a` is not a
# unique mode or not a mode.
#
# To avoid the need of looping through all numbers that are not `a`, we can
# maintain the sums that are not related to `a` in the loop.
#
# So, during the simplification of the formula, keep the running sums of
# pss, spp, pp, ss, and ps as the first item.
# (for cleaner notation, abbreviate p[b] and s[b] to just p and s)
#
#   sum(b != a) (p[a] * p * s) * (r - s[a] - s)
#             + (s[a] * s * p) * (l - p[a] - p)
#             + (p, 2) * s[a] * (r - s[a])
#             + (s, 2) * p[a] * (l - p[a])
#
#   sum(b != a) (p * s) * (p[a] * (r - s[a])) + (p * s^2) * (-p[a])
#             + (s * p) * (s[a] * (l - p[a])) + (s * p^2) * (-s[a])
#             + (p^2 - p) * (s[a] * (r - s[a]) / 2)
#             + (s^2 - s) * (p[a] * (l - p[a]) / 2)

class Solution:
  # Same as 3395. Subsequences with a Unique Middle Mode I
  def subsequencesWithMiddleMode(self, nums: list[int]) -> int:
    MOD = 1_000_000_007
    ans = 0
    p = collections.Counter()  # prefix counter
    s = collections.Counter(nums)  # suffix counter

    def nC2(n: int) -> int:
      return n * (n - 1) // 2

    pss = 0
    spp = 0
    pp = 0
    ss = sum(freq**2 for freq in s.values())
    ps = 0

    for i, a in enumerate(nums):
      # Update running sums after decrementing s[a].
      pss += p[a] * (-s[a]**2 + (s[a] - 1)**2)
      spp += -p[a]**2  # (-s[a] + (s[a] - 1)) * p[a]**2
      ss += -s[a]**2 + (s[a] - 1)**2
      ps += -p[a]  # -p[a] * (-s[a] + (s[a] - 1))

      s[a] -= 1

      l = i
      r = len(nums) - i - 1

      # Start with all possible subsequences with `a` as the middle number.
      ans += nC2(l) * nC2(r)

      # Minus the cases where the frequency of `a` is 1, so it's not a mode.
      ans -= nC2(l - p[a]) * nC2(r - s[a])

      # Minus the values where `b != a`.
      pss_ = pss - p[a] * s[a]**2
      spp_ = spp - s[a] * p[a]**2
      pp_ = pp - p[a]**2
      ss_ = ss - s[a]**2
      ps_ = ps - p[a] * s[a]
      p_ = l - p[a]
      s_ = r - s[a]

      # Minus the cases where the `a` is not a "unique" mode or not a mode.
      ans -= ps_ * (p[a] * (r - s[a])) + pss_ * (-p[a])
      ans -= ps_ * (s[a] * (l - p[a])) + spp_ * (-s[a])
      ans -= (pp_ - p_) * s[a] * (r - s[a]) // 2
      ans -= (ss_ - s_) * p[a] * (l - p[a]) // 2
      ans %= MOD

      # Update running sums after incrementing p[a].
      pss += s[a]**2  # (-p[a] + (p[a] + 1)) * s[a]**2
      spp += s[a] * (-p[a]**2 + (p[a] + 1)**2)
      pp += -p[a]**2 + (p[a] + 1)**2
      ps += s[a]  # (-p[a] + (p[a] + 1)) * s[a]

      p[a] += 1

    return ans


You are an expert Python programmer. Write correct, efficient Python 3 code that solves the problem and passes all tests. Output the code in the following format: ```python
<code>
```

PROBLEM:
You are given an m x n grid. A robot starts at the top-left corner of the grid (0, 0) and wants to reach the bottom-right corner (m - 1, n - 1). The robot can move either right or down at any point in time.
The grid contains a value coins[i][j] in each cell:

If coins[i][j] >= 0, the robot gains that many coins.
If coins[i][j] < 0, the robot encounters a robber, and the robber steals the absolute value of coins[i][j] coins.

The robot has a special ability to neutralize robbers in at most 2 cells on its path, preventing them from stealing coins in those cells.
Note: The robot's total coins can be negative.
Return the maximum profit the robot can gain on the route.

Example 1:

Input: coins = [[0,1,-1],[1,-2,3],[2,-3,4]]
Output: 8
Explanation:
An optimal path for maximum coins is:

Start at (0, 0) with 0 coins (total coins = 0).
Move to (0, 1), gaining 1 coin (total coins = 0 + 1 = 1).
Move to (1, 1), where there's a robber stealing 2 coins. The robot uses one neutralization here, avoiding the robbery (total coins = 1).
Move to (1, 2), gaining 3 coins (total coins = 1 + 3 = 4).
Move to (2, 2), gaining 4 coins (total coins = 4 + 4 = 8).

Example 2:

Input: coins = [[10,10,10],[10,10,10]]
Output: 40
Explanation:
An optimal path for maximum coins is:

Start at (0, 0) with 10 coins (total coins = 10).
Move to (0, 1), gaining 10 coins (total coins = 10 + 10 = 20).
Move to (0, 2), gaining another 10 coins (total coins = 20 + 10 = 30).
Move to (1, 2), gaining the final 10 coins (total coins = 30 + 10 = 40).

Constraints:

m == coins.length
n == coins[i].length
1 <= m, n <= 500
-1000 <= coins[i][j] <= 1000

Your solution to the problem should be a method of the class Solution called maximumAmount and should pass all tests. Use the following starter code:
```python
class Solution:
    def maximumAmount(self, coins: List[List[int]]) -> int:
        # Add your code here
```

SOLUTION:

Question:
PROBLEM:
You are given an m x n grid. A robot starts at the top-left corner of the grid (0, 0) and wants to reach the bottom-right corner (m - 1, n - 1). The robot can move either right or down at any point in time.
The grid contains a value coins[i][j] in each cell:

If coins[i][j] >= 0, the robot gains that many coins.
If coins[i][j] < 0, the robot encounters a robber, and the robber steals the absolute value of coins[i][j] coins.

The robot has a special ability to neutralize robbers in at most 2 cells on its path, preventing them from stealing coins in those cells.
Note: The robot's total coins can be negative.
Return the maximum profit the robot can gain on the route.

Example 1:

Input: coins = [[0,1,-1],[1,-2,3],[2,-3,4]]
Output: 8
Explanation:
An optimal path for maximum coins is:

Start at (0, 0) with 0 coins (total coins = 0).
Move to (0, 1), gaining 1 coin (total coins = 0 + 1 = 1).
Move to (1, 1), where there's a robber stealing 2 coins. The robot uses one neutralization here, avoiding the robbery (total coins = 1).
Move to (1, 2), gaining 3 coins (total coins = 1 + 3 = 4).
Move to (2, 2), gaining 4 coins (total coins = 4 + 4 = 8).

Example 2:

Input: coins = [[10,10,10],[10,10,10]]
Output: 40
Explanation:
An optimal path for maximum coins is:

Start at (0, 0) with 10 coins (total coins = 10).
Move to (0, 1), gaining 10 coins (total coins = 10 + 10 = 20).
Move to (0, 2), gaining another 10 coins (total coins = 20 + 10 = 30).
Move to (1, 2), gaining the final 10 coins (total coins = 30 + 10 = 40).

Constraints:

m == coins.length
n == coins[i].length
1 <= m, n <= 500
-1000 <= coins[i][j] <= 1000

Your solution to the problem should be a method of the class Solution called maximumAmount and should pass all tests. Use the following starter code:
```python
class Solution:
    def maximumAmount(self, coins: List[List[int]]) -> int:
        # Add your code here
```

SOLUTION:

Answer: assert Solution().maximumAmount(coins = [[0,1,-1],[1,-2,3],[2,-3,4]]) == 8

Answer: assert Solution().maximumAmount(coins = [[1,2,3,4,5],[5,4,3,2,1],[1,3,5,7,9]]) == 34

Answer: assert Solution().maximumAmount(coins = [[1000, -1000, 1000, -1000], [-1000, 1000, -1000, 1000], [1000, -1000, 1000, -1000], [-1000, 1000, -1000, 1000]]) == 3000

Answer: assert Solution().maximumAmount(coins = [[10,10,10],[10,10,10]]) == 40

Answer: assert Solution().maximumAmount(coins = [[1, 2, 3, 4, 5], [5, 4, 3, 2, 1], [1, 3, 5, 7, 9], [9, 7, 5, 3, 1]]) == 35

Answer: assert Solution().maximumAmount(coins = [[-1,-2,-3],[-4,-5,-6],[-7,-8,-9]]) == -6

Answer: assert Solution().maximumAmount(coins = [[-1,0,1],[-2,0,2],[-3,0,3]]) == 6

Answer: assert Solution().maximumAmount(coins = [[100, -100, 50], [-50, 200, -300], [400, -500, 600]]) == 1100

Answer: assert Solution().maximumAmount(coins = [[1,-1,1,-1],[1,-1,1,-1],[-1,1,-1,1]]) == 4

Answer: assert Solution().maximumAmount(coins = [[100,-50,200],[-300,400,50],[-600,700,-800]]) == 1200

Answer: assert Solution().maximumAmount(coins = [[1, -1, 2], [2, -2, 3], [3, -3, 4]]) == 10

Answer: assert Solution().maximumAmount(coins = [[100, -50, 200], [-300, 400, -500], [600, -700, 800]]) == 1500

Answer: assert Solution().maximumAmount(coins = [[1,2,3,4,5],[5,4,3,2,1],[1,3,5,7,9],[9,7,5,3,1],[1,1,1,1,1]]) == 36

Answer: assert Solution().maximumAmount(coins = [[-10, -20, -30], [-40, -50, -60], [-70, -80, -90], [100, 200, 300], [400, 500, 600]]) == 1590

Answer: assert Solution().maximumAmount(coins = [[0, 1, -2, 3, -4], [-5, 6, -7, 8, -9], [10, -11, 12, -13, 14], [-15, 16, -17, 18, -19], [20, -21, 22, -23, 24]]) == 58

Answer: assert Solution().maximumAmount(coins = [[-100, 200, -300, 400, -500], [600, -700, 800, -900, 1000], [-200, 300, -400, 500, -600], [700, -800, 900, -1000, 1100]]) == 3000

Answer: assert Solution().maximumAmount(coins = [[-5, -10, -15], [-20, -25, -30], [-35, -40, -45], [-50, -55, -60]]) == -60

Answer: assert Solution().maximumAmount(coins = [[-1, 1, -1, 1, -1, 1], [1, -1, 1, -1, 1, -1], [-1, 1, -1, 1, -1, 1], [1, -1, 1, -1, 1, -1], [-1, 1, -1, 1, -1, 1]]) == 2

Answer: assert Solution().maximumAmount(coins = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]) == 0

Answer: assert Solution().maximumAmount(coins = [[-1, 2, -3, 4, -5, 6, -7], [8, -9, 10, -11, 12, -13, 14], [-15, 16, -17, 18, -19, 20, -21], [22, -23, 24, -25, 26, -27, 28], [-29, 30, -31, 32, -33, 34, -35], [36, -37, 38, -39, 40, -41, 42]]) == 110

Answer: assert Solution().maximumAmount(coins = [[1000, -900, 800, -700], [-600, 500, -400, 300], [200, -100, 0, 100], [-200, 300, -400, 500]]) == 2700

Answer: assert Solution().maximumAmount(coins = [[10, -20, 30, -40], [-10, 20, -30, 40], [50, -60, 70, -80], [-90, 100, -110, 120]]) == 270

Answer: assert Solution().maximumAmount(coins = [[-1, -2, -3, 10], [5, -1, 2, -1], [3, -2, -1, 4], [-1, 2, 1, 5]]) == 17

Answer: assert Solution().maximumAmount(coins = [[1000, -1000, 500, -500, 250, -250], [200, -200, 300, -300, 400, -400], [50, -50, 60, -60, 70, -70], [-80, 80, -90, 90, -100, 100]]) == 2300

Answer: assert Solution().maximumAmount(coins = [[-10, -20, -30], [-40, -50, -60], [-70, -80, -90]]) == -60

Answer: assert Solution().maximumAmount(coins = [[1, -2, 3, -4, 5], [5, -4, 3, -2, 1], [2, 4, -6, 8, -10], [-12, 14, -16, 18, -20], [21, -22, 23, -24, 25]]) == 74

Answer: assert Solution().maximumAmount(coins = [[-50, 50, -50, 50, -50], [50, -50, 50, -50, 50], [-50, 50, -50, 50, -50], [50, -50, 50, -50, 50], [-50, 50, -50, 50, 50]]) == 150

Answer: assert Solution().maximumAmount(coins = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [-1, -2, -3, -4, -5, -6, -7, -8, -9, -10], [10, 9, 8, 7, 6, 5, 4, 3, 2, 1], [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]]) == 56

Answer: assert Solution().maximumAmount(coins = [[100, 200, 300, 400, 500], [500, 400, 300, 200, 100], [100, 300, 500, 700, 900], [900, 700, 500, 300, 100], [100, 200, 300, 400, 500]]) == 4000

Answer: assert Solution().maximumAmount(coins = [[1, -1, 1, -1, 1, -1], [-1, 1, -1, 1, -1, 1], [1, -1, 1, -1, 1, -1], [-1, 1, -1, 1, -1, 1], [1, -1, 1, -1, 1, -1], [-1, 1, -1, 1, -1, 1]]) == 3

Answer: assert Solution().maximumAmount(coins = [[10, 20, 30, 40, 50], [-10, -20, -30, -40, -50], [15, 25, 35, 45, 55], [-15, -25, -35, -45, -55], [60, 70, 80, 90, 100]]) == 425

Answer: assert Solution().maximumAmount(coins = [[0, -1, 2, -3, 4, -5], [-6, 7, -8, 9, -10, 11], [12, -13, 14, -15, 16, -17], [-18, 19, -20, 21, -22, 23], [24, -25, 26, -27, 28, -29], [-30, 31, -32, 33, -34, 35]]) == 86

Answer: assert Solution().maximumAmount(coins = [[500, -500, 500, -500, 500], [-500, 500, -500, 500, -500], [500, -500, 500, -500, 500], [-500, 500, -500, 500, -500]]) == 1000

Answer: assert Solution().maximumAmount(coins = [[1000, -1000, 500, -500, 0], [-500, 1000, -1500, 2000, -2500], [3000, -3500, 4000, -4500, 5000], [-5500, 6000, -6500, 7000, -7500], [8000, -8500, 9000, -9500, 10000]]) == 25000

Answer: assert Solution().maximumAmount(coins = [[10, -10, 20, -20, 30], [30, -30, 40, -40, 50], [50, -50, 60, -60, 70], [70, -70, 80, -80, 90], [90, -90, 100, -100, 110]]) == 460

Answer: assert Solution().maximumAmount(coins = [[-100, 200, -300, 400], [500, -600, 700, -800], [-900, 1000, -1100, 1200], [1300, -1400, 1500, -1600]]) == 2300

Answer: assert Solution().maximumAmount(coins = [[-10, 20, -30, 40], [50, -60, 70, -80], [90, -100, 110, -120], [130, -140, 150, -160]]) == 410

Answer: assert Solution().maximumAmount(coins = [[100, 200, -300, 400, -500, 600], [-700, 800, -900, 1000, -1100, 1200], [1300, -1400, 1500, -1600, 1700, -1800]]) == 3400

Answer: assert Solution().maximumAmount(coins = [[1, -2, 3, -4, 5], [-1, 2, -3, 4, -5], [1, -2, 3, -4, 5], [-1, 2, -3, 4, -5], [1, -2, 3, -4, 5]]) == 13

Answer: assert Solution().maximumAmount(coins = [[1, -1, 2, -2], [-3, 3, -4, 4], [5, -5, 6, -6], [-7, 7, -8, 8]]) == 18

Answer: assert Solution().maximumAmount(coins = [[0, -1, 0, -1, 0], [1, 0, -1, 0, 1], [0, -1, 0, -1, 0], [1, 0, -1, 0, 1], [0, -1, 0, -1, 0]]) == 3

Answer: assert Solution().maximumAmount(coins = [[100, 200, 300, 400], [-100, -200, -300, -400], [500, -600, 700, -800], [-900, 1000, -1100, 1200]]) == 2700

Answer: assert Solution().maximumAmount(coins = [[-100, -200, -300, -400], [-500, -600, -700, -800], [-900, -1000, -1100, -1200], [-1300, -1400, -1500, -1600]]) == -1800

Answer: assert Solution().maximumAmount(coins = [[-100, 50, -30, 10, -10], [20, -30, 40, -50, 60], [-70, 80, -90, 100, -110], [120, -130, 140, -150, 160]]) == 310

Answer: assert Solution().maximumAmount(coins = [[-100, 100, -200, 200, -300], [-400, 500, -600, 700, -800], [900, -1000, 1100, -1200, 1300], [-1400, 1500, -1600, 1700, -1800], [1900, -2000, 2100, -2200, 2300]]) == 5400

Answer: assert Solution().maximumAmount(coins = [[-10, -20, -30, -40], [-50, -60, -70, -80], [-90, -100, -110, -120], [-130, -140, -150, 160]]) == 60

Answer: assert Solution().maximumAmount(coins = [[-10, 10, -20, 20, -30, 30, -40, 40], [10, -10, 20, -20, 30, -30, 40, -40], [-10, 10, -20, 20, -30, 30, -40, 40], [10, -10, 20, -20, 30, -30, 40, -40]]) == 70

Answer: assert Solution().maximumAmount(coins = [[10, -15, 20, -25, 30], [-35, 40, -45, 50, -55], [60, -65, 70, -75, 80], [-85, 90, -95, 100, -105], [110, -115, 120, -125, 130]]) == 310

Answer: assert Solution().maximumAmount(coins = [[0, 0, 0, 0, 0], [0, -1, -2, -3, 0], [0, -4, -5, -6, 0], [0, -7, -8, -9, 0], [0, 0, 0, 0, 0]]) == 0

Answer: assert Solution().maximumAmount(coins = [[0, 1, -1, 2, -2], [-3, 4, -5, 6, -7], [8, -9, 10, -11, 12], [-13, 14, -15, 16, -17], [18, -19, 20, -21, 22]]) == 52

Answer: assert Solution().maximumAmount(coins = [[1000, -500, 200, -100], [-200, 300, -400, 500], [600, -700, 800, -900], [100, 200, 300, 400]]) == 3100

Answer: assert Solution().maximumAmount(coins = [[-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, 10]]) == 6

Answer: assert Solution().maximumAmount(coins = [[1, -1, 2, -2, 3, -3, 4, -4, 5], [-5, 6, -7, 8, -9, 10, -11, 12, -13], [14, -15, 16, -17, 18, -19, 20, -21, 22], [-23, 24, -25, 26, -27, 28, -29, 30, -31], [32, -33, 34, -35, 36, -37, 38, -39, 40]]) == 111

Answer: assert Solution().maximumAmount(coins = [[-1, -2, -3, 4, 5], [3, -1, -2, -3, 4], [1, 2, -1, -2, 3], [-1, 2, 3, -1, 2], [2, -1, 1, 2, -1]]) == 16

Answer: assert Solution().maximumAmount(coins = [[1, -1, 1, -1, 1], [-1, 1, -1, 1, -1], [1, -1, 1, -1, 1], [-1, 1, -1, 1, -1], [1, -1, 1, -1, 1]]) == 3

Answer: assert Solution().maximumAmount(coins = [[10, 20, 30, 40, 50, 60], [-10, -20, -30, -40, -50, -60], [10, 20, 30, 40, 50, 60], [-10, -20, -30, -40, -50, -60], [10, 20, 30, 40, 50, 60], [-10, -20, -30, -40, -50, -60]]) == 270

Answer: assert Solution().maximumAmount(coins = [[10, -1, 20, -2, 30], [-3, 40, -5, 60, -7], [8, -9, 100, -11, 120], [-13, 140, -15, 160, -17], [18, -19, 200, -21, 220]]) == 600

Answer: assert Solution().maximumAmount(coins = [[-100, 200, -300, 400, -500], [600, -700, 800, -900, 1000], [1100, -1200, 1300, -1400, 1500]]) == 4400

Answer: assert Solution().maximumAmount(coins = [[1000, -1000, 1000, -1000, 1000], [-1000, 1000, -1000, 1000, -1000], [1000, -1000, 1000, -1000, 1000], [-1000, 1000, -1000, 1000, -1000], [1000, -1000, 1000, -1000, 1000]]) == 3000

Answer: assert Solution().maximumAmount(coins = [[1, 2, 3, 4, 5], [5, 4, 3, 2, 1], [1, 3, 5, 7, 9], [9, 7, 5, 3, 1], [2, 4, 6, 8, 10]]) == 52

Answer: assert Solution().maximumAmount(coins = [[-1, -1, -1, -1, -1], [1, 1, 1, 1, 1], [-1, -1, -1, -1, -1], [1, 1, 1, 1, 1], [-1, -1, -1, -1, -1]]) == 5

Answer: assert Solution().maximumAmount(coins = [[-5, -4, -3, -2, -1], [1, 2, 3, 4, 5], [-5, -4, -3, -2, -1], [1, 2, 3, 4, 5], [-5, -4, -3, -2, -1]]) == 19

Answer: assert Solution().maximumAmount(coins = [[-100, 100, -200, 200, -300], [300, -400, 500, -600, 700], [800, -900, 1000, -1100, 1200]]) == 3200

Answer: assert Solution().maximumAmount(coins = [[1000, -1000, 2000, -2000], [-2000, 3000, -3000, 4000], [4000, -5000, 6000, -7000], [-8000, 9000, -10000, 11000]]) == 23000

Answer: assert Solution().maximumAmount(coins = [[1, -2, 3, -4, 5], [-5, 6, -7, 8, -9], [9, -10, 11, -12, 13], [-14, 15, -16, 17, -18], [19, -20, 21, -22, 23]]) == 54

Answer: assert Solution().maximumAmount(coins = [[-100, 200, -300, 400, -500], [600, -700, 800, -900, 1000], [-1100, 1200, -1300, 1400, -1500], [1600, -1700, 1800, -1900, 2000], [-2100, 2200, -2300, 2400, -2500]]) == 3900

Answer: assert Solution().maximumAmount(coins = [[-100, -100, -100, -100], [-100, 100, 100, -100], [-100, 100, 100, -100], [-100, -100, -100, -100]]) == 100

Answer: assert Solution().maximumAmount(coins = [[-10, -20, 30, -40, 50], [60, -70, 80, -90, 100], [-110, 120, -130, 140, -150], [160, -170, 180, -190, 200]]) == 480

Answer: assert Solution().maximumAmount(coins = [[1, -2, 3, -4, 5, -6], [-7, 8, -9, 10, -11, 12], [13, -14, 15, -16, 17, -18], [-19, 20, -21, 22, -23, 24]]) == 59

Answer: assert Solution().maximumAmount(coins = [[10, 20, 30, 40, 50], [-60, -70, -80, -90, -100], [110, 120, 130, 140, 150], [-160, -170, -180, -190, -200], [210, 220, 230, 240, 250]]) == 1270

Answer: assert Solution().maximumAmount(coins = [[-5,-6,-7],[8,9,-10],[-11,-12,13]]) == 30

Answer: assert Solution().maximumAmount(coins = [[-10, 15, -20, 25], [10, -5, 30, -35], [5, 20, -10, 15], [-15, 25, -30, 40]]) == 100

Answer: assert Solution().maximumAmount(coins = [[100, 200, 300, 400, 500, -100, -200, -300, -400, -500], [-500, -100, 200, 600, -300, 400, 700, -800, 900, -1000], [1100, -1200, 1300, -1400, 1500, -1600, 1700, -1800, 1900, -2000], [-2100, 2200, -2300, 2400, -2500, 2600, -2700, 2800, -2900, 3000]]) == 10000

Answer: assert Solution().maximumAmount(coins = [[-1, -2, -3, 4, 5], [6, 7, 8, -9, 10], [-11, 12, -13, 14, -15], [16, -17, 18, -19, 20]]) == 62

Answer: assert Solution().maximumAmount(coins = [[0, -1, 2, -3, 4, -5], [5, -4, 3, -2, 1, 0], [-1, 2, -3, 4, -5, 6], [6, -5, 4, -3, 2, -1], [-2, 3, -4, 5, -6, 7]]) == 23

Answer: assert Solution().maximumAmount(coins = [[-50, -50, -50, -50], [-50, 50, 50, 50], [50, 50, -50, 50], [50, 50, 50, -50]]) == 150

Answer: assert Solution().maximumAmount(coins = [[-1, -1, -1, -1], [-1, 1, -1, 1], [-1, -1, 1, -1], [1, -1, 1, -1]]) == 1

Answer: assert Solution().maximumAmount(coins = [[1, 2, 3, 4, 5], [-1, -2, -3, -4, -5], [1, 2, 3, 4, 5], [-1, -2, -3, -4, -5], [1, 2, 3, 4, 5]]) == 25

Answer: assert Solution().maximumAmount(coins = [[-1, 2, -3, 4, -5], [5, -6, 7, -8, 9], [9, -10, 11, -12, 13], [13, -14, 15, -16, 17], [17, -18, 19, -20, 21]]) == 83

Answer: assert Solution().maximumAmount(coins = [[-1, -2, -3, -4, -5, -6], [7, 8, 9, 10, 11, 12], [-13, -14, -15, -16, -17, -18], [19, 20, 21, 22, 23, 24], [-25, -26, -27, -28, -29, -30]]) == 135

Answer: assert Solution().maximumAmount(coins = [[10, 20, -30, 40, -50, 60, -70], [80, -90, 100, -110, 120, -130, 140], [-150, 160, -170, 180, -190, 200, -210]]) == 370

Answer: assert Solution().maximumAmount(coins = [[-5, -10, 15, 20], [10, -20, 25, -30], [-15, 25, -35, 40], [50, -60, 70, -80]]) == 110

Answer: assert Solution().maximumAmount(coins = [[50, -50, 50, -50, 50], [-50, 50, -50, 50, -50], [50, -50, 50, -50, 50], [-50, 50, -50, 50, -50], [50, -50, 50, -50, 50]]) == 150

Answer: assert Solution().maximumAmount(coins = [[-100, 200, -300, 400], [-500, 600, -700, 800], [900, -1000, 1100, -1200], [-1300, 1400, -1500, 1600]]) == 3700

Answer: assert Solution().maximumAmount(coins = [[-10, 20, -30, 40], [50, -60, 70, -80], [-90, 100, -110, 120], [130, -140, 150, -160]]) == 230

import random
import functools
import collections
import string
import math
import datetime

from typing import *
from functools import *
from collections import *
from itertools import *
from heapq import *
from bisect import *
from string import *
from operator import *
from math import *

inf = float('inf')

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def list_node(values: list):
    if not values:
        return None
    head = ListNode(values[0])
    p = head
    for val in values[1:]:
        node = ListNode(val)
        p.next = node
        p = node
    return head

def is_same_list(p1, p2):
    if p1 is None and p2 is None:
        return True
    if not p1 or not p2:
        return False
    return p1.val == p2.val and is_same_list(p1.next, p2.next)

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def tree_node(values: list):
    if not values:
        return None
    root = TreeNode(values[0])
    i = 1
    queue = deque()
    queue.append(root)
    while queue:
        node = queue.popleft()
        if i < len(values) and values[i] is not None:
            node.left = TreeNode(values[i])
            queue.append(node.left)
        i += 1
        if i < len(values) and values[i] is not None:
            node.right = TreeNode(values[i])
            queue.append(node.right)
        i += 1
    return root

def is_same_tree(p, q):
    if not p and not q:
        return True
    elif not p or not q:
        return False
    elif p.val != q.val:
        return False
    else:
        return is_same_tree(p.left, q.left) and is_same_tree(p.right, q.right)

class Solution:
    def maximumAmount(self, coins: List[List[int]]) -> int:
        @cache
        def dfs(i: int, j: int, k: int) -> int:
            if i >= m or j >= n:
                return -inf
            if i == m - 1 and j == n - 1:
                return max(coins[i][j], 0) if k else coins[i][j]
            ans = coins[i][j] + max(dfs(i + 1, j, k), dfs(i, j + 1, k))
            if coins[i][j] < 0 and k:
                ans = max(ans, dfs(i + 1, j, k - 1), dfs(i, j + 1, k - 1))
            return ans

        m, n = len(coins), len(coins[0])
        return dfs(0, 0, 2)


You are an expert Python programmer. Write correct, efficient Python 3 code that solves the problem and passes all tests. Output the code in the following format: ```python
<code>
```

PROBLEM:
You are given an array nums of n integers and an integer k.
For each subarray of nums, you can apply up to k operations on it. In each operation, you increment any element of the subarray by 1.
Note that each subarray is considered independently, meaning changes made to one subarray do not persist to another.
Return the number of subarrays that you can make non-decreasing after performing at most k operations.
An array is said to be non-decreasing if each element is greater than or equal to its previous element, if it exists.

Example 1:

Input: nums = [6,3,1,2,4,4], k = 7
Output: 17
Explanation:
Out of all 21 possible subarrays of nums, only the subarrays [6, 3, 1], [6, 3, 1, 2], [6, 3, 1, 2, 4] and [6, 3, 1, 2, 4, 4] cannot be made non-decreasing after applying up to k = 7 operations. Thus, the number of non-decreasing subarrays is 21 - 4 = 17.

Example 2:

Input: nums = [6,3,1,3,6], k = 4
Output: 12
Explanation:
The subarray [3, 1, 3, 6] along with all subarrays of nums with three or fewer elements, except [6, 3, 1], can be made non-decreasing after k operations. There are 5 subarrays of a single element, 4 subarrays of two elements, and 2 subarrays of three elements except [6, 3, 1], so there are 1 + 5 + 4 + 2 = 12 subarrays that can be made non-decreasing.

Constraints:

1 <= nums.length <= 105
1 <= nums[i] <= 109
1 <= k <= 109

Your solution to the problem should be a method of the class Solution called countNonDecreasingSubarrays and should pass all tests. Use the following starter code:
```python
class Solution:
    def countNonDecreasingSubarrays(self, nums: List[int], k: int) -> int:
        # Add your code here
```

SOLUTION:

Question:
PROBLEM:
You are given an array nums of n integers and an integer k.
For each subarray of nums, you can apply up to k operations on it. In each operation, you increment any element of the subarray by 1.
Note that each subarray is considered independently, meaning changes made to one subarray do not persist to another.
Return the number of subarrays that you can make non-decreasing after performing at most k operations.
An array is said to be non-decreasing if each element is greater than or equal to its previous element, if it exists.

Example 1:

Input: nums = [6,3,1,2,4,4], k = 7
Output: 17
Explanation:
Out of all 21 possible subarrays of nums, only the subarrays [6, 3, 1], [6, 3, 1, 2], [6, 3, 1, 2, 4] and [6, 3, 1, 2, 4, 4] cannot be made non-decreasing after applying up to k = 7 operations. Thus, the number of non-decreasing subarrays is 21 - 4 = 17.

Example 2:

Input: nums = [6,3,1,3,6], k = 4
Output: 12
Explanation:
The subarray [3, 1, 3, 6] along with all subarrays of nums with three or fewer elements, except [6, 3, 1], can be made non-decreasing after k operations. There are 5 subarrays of a single element, 4 subarrays of two elements, and 2 subarrays of three elements except [6, 3, 1], so there are 1 + 5 + 4 + 2 = 12 subarrays that can be made non-decreasing.

Constraints:

1 <= nums.length <= 105
1 <= nums[i] <= 109
1 <= k <= 109

Your solution to the problem should be a method of the class Solution called countNonDecreasingSubarrays and should pass all tests. Use the following starter code:
```python
class Solution:
    def countNonDecreasingSubarrays(self, nums: List[int], k: int) -> int:
        # Add your code here
```

SOLUTION:

assert Solution().countNonDecreasingSubarrays(nums = [5,4,3,2,1], k = 10) == 15

assert Solution().countNonDecreasingSubarrays(nums = [10,20,10,20,10], k = 15) == 13

assert Solution().countNonDecreasingSubarrays(nums = [1,2,3,4,5,6,7,8,9,10], k = 1) == 55

assert Solution().countNonDecreasingSubarrays(nums = [1,100,2,99,3,98], k = 100) == 17

assert Solution().countNonDecreasingSubarrays(nums = [100,50,25,12,6,3,1], k = 50) == 17

assert Solution().countNonDecreasingSubarrays(nums = [6,3,1,2,4,4], k = 7) == 17

assert Solution().countNonDecreasingSubarrays(nums = [1,3,2,4,3,5], k = 5) == 21

assert Solution().countNonDecreasingSubarrays(nums = [1,2,3,4,5], k = 3) == 15

assert Solution().countNonDecreasingSubarrays(nums = [1,2,2,3,4,4,5], k = 6) == 28

assert Solution().countNonDecreasingSubarrays(nums = [3,2,1,4,5,6], k = 3) == 21

assert Solution().countNonDecreasingSubarrays(nums = [1,10,3,4,2,5], k = 8) == 15

assert Solution().countNonDecreasingSubarrays(nums = [10,9,8,7,6,5,4,3,2,1], k = 15) == 45

assert Solution().countNonDecreasingSubarrays(nums = [1,2,3], k = 0) == 6

assert Solution().countNonDecreasingSubarrays(nums = [9,7,5,3,1], k = 5) == 9

assert Solution().countNonDecreasingSubarrays(nums = [1], k = 1) == 1

assert Solution().countNonDecreasingSubarrays(nums = [3,2,1,2,3], k = 4) == 15

assert Solution().countNonDecreasingSubarrays(nums = [10,10,10,10,10], k = 0) == 15

assert Solution().countNonDecreasingSubarrays(nums = [6,3,1,3,6], k = 4) == 12

assert Solution().countNonDecreasingSubarrays(nums = [1,3,5,2,4,6], k = 5) == 21

assert Solution().countNonDecreasingSubarrays(nums = [1,3,5,7,9], k = 1) == 15

assert Solution().countNonDecreasingSubarrays(nums = [1,3,5,7,9], k = 0) == 15

assert Solution().countNonDecreasingSubarrays(nums = [1,2,2,3,3,3,4], k = 5) == 28

assert Solution().countNonDecreasingSubarrays(nums = [1,1,1,1,1], k = 1) == 15

assert Solution().countNonDecreasingSubarrays(nums = [9,7,5,3,1], k = 15) == 14

assert Solution().countNonDecreasingSubarrays(nums = [1,1,1,1,1], k = 0) == 15

assert Solution().countNonDecreasingSubarrays(nums = [10,9,8,7,6,5,4,3,2,1], k = 50) == 55

assert Solution().countNonDecreasingSubarrays(nums = [1,3,5,7,9], k = 5) == 15

assert Solution().countNonDecreasingSubarrays(nums = [7,8,9,10,1,2,3,4,5,6], k = 20) == 39

assert Solution().countNonDecreasingSubarrays(nums = [10,9,8,7,6,5,4,3,2,1], k = 25) == 49

assert Solution().countNonDecreasingSubarrays(nums = [1,3,2,5,4,6,7], k = 5) == 28

assert Solution().countNonDecreasingSubarrays(nums = [5, 3, 8, 6, 10, 9, 14, 12, 16, 15], k = 50) == 55

assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 3, 2, 1, 2, 3, 4, 3, 2, 1], k = 15) == 66

assert Solution().countNonDecreasingSubarrays(nums = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1], k = 45) == 55

assert Solution().countNonDecreasingSubarrays(nums = [10, 20, 10, 30, 20, 40, 30, 50, 40, 60], k = 25) == 43

assert Solution().countNonDecreasingSubarrays(nums = [1, 5, 3, 7, 8, 2, 6, 4, 9, 10], k = 15) == 55

assert Solution().countNonDecreasingSubarrays(nums = [1,100,2,99,3,98,4,97,5,96], k = 500) == 55

assert Solution().countNonDecreasingSubarrays(nums = [3,1,2,1,3,2,1,3,2,1], k = 30) == 55

assert Solution().countNonDecreasingSubarrays(nums = [10, 20, 10, 20, 10, 20, 10, 20, 10, 20], k = 50) == 55

assert Solution().countNonDecreasingSubarrays(nums = [5, 3, 8, 6, 7, 2, 4, 9, 1, 10], k = 20) == 49

assert Solution().countNonDecreasingSubarrays(nums = [1, 10, 2, 9, 3, 8, 4, 7, 5, 6], k = 20) == 45

assert Solution().countNonDecreasingSubarrays(nums = [1,1,2,2,3,3,4,4,5,5,6,6], k = 15) == 78

assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1], k = 20) == 120

assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3], k = 10) == 111

assert Solution().countNonDecreasingSubarrays(nums = [1, 3, 2, 4, 3, 5, 4, 6, 5, 7], k = 10) == 55

assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2], k = 5) == 55

assert Solution().countNonDecreasingSubarrays(nums = [1, 3, 2, 3, 2, 4, 3, 5, 4, 6], k = 12) == 55

assert Solution().countNonDecreasingSubarrays(nums = [5, 3, 1, 6, 4, 2, 7, 5, 3, 8, 6, 4, 9, 7, 5], k = 75) == 120

assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1], k = 15) == 120

assert Solution().countNonDecreasingSubarrays(nums = [5, 5, 5, 5, 5, 5, 5, 5, 5, 5], k = 0) == 55

assert Solution().countNonDecreasingSubarrays(nums = [1,1,1,1,1,1,1,1,1,1], k = 5) == 55

assert Solution().countNonDecreasingSubarrays(nums = [3, 1, 2, 1, 3, 2, 1, 2, 3, 2, 1, 2, 3], k = 15) == 91

assert Solution().countNonDecreasingSubarrays(nums = [1, 10, 100, 1000, 10000], k = 10000) == 15

assert Solution().countNonDecreasingSubarrays(nums = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], k = 5) == 55

assert Solution().countNonDecreasingSubarrays(nums = [10, 5, 15, 20, 25, 5, 10, 30, 40, 50], k = 50) == 55

assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], k = 3) == 55

assert Solution().countNonDecreasingSubarrays(nums = [10,9,8,7,6,5,4,3,2,1], k = 45) == 55

assert Solution().countNonDecreasingSubarrays(nums = [1,2,1,2,1,2,1,2,1,2,1,2,1,2,1], k = 25) == 120

assert Solution().countNonDecreasingSubarrays(nums = [10, 1, 10, 1, 10, 1, 10, 1, 10, 1], k = 10) == 30

assert Solution().countNonDecreasingSubarrays(nums = [3, 3, 3, 3, 3, 3, 3, 3, 3, 3], k = 10) == 55

assert Solution().countNonDecreasingSubarrays(nums = [5, 4, 3, 2, 1, 0, -1, -2, -3, -4], k = 30) == 52

assert Solution().countNonDecreasingSubarrays(nums = [5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1], k = 50) == 120

assert Solution().countNonDecreasingSubarrays(nums = [1,1,1,1,1,1,1,1,1,1], k = 0) == 55

assert Solution().countNonDecreasingSubarrays(nums = [5,10,15,20,25,30,35,40,45,50], k = 100) == 55

assert Solution().countNonDecreasingSubarrays(nums = [1,2,3,2,1,2,3,4,5,6], k = 15) == 55

assert Solution().countNonDecreasingSubarrays(nums = [1, 100, 2, 99, 3, 98, 4, 97, 5, 96], k = 150) == 31

assert Solution().countNonDecreasingSubarrays(nums = [1, 10, 2, 9, 3, 8, 4, 7, 5, 6], k = 25) == 49

assert Solution().countNonDecreasingSubarrays(nums = [3,3,3,3,3,3,3,3,3,3], k = 100) == 55

assert Solution().countNonDecreasingSubarrays(nums = [5,4,5,4,5,4,5,4,5,4], k = 20) == 55

assert Solution().countNonDecreasingSubarrays(nums = [5,5,5,5,5,5,5,5,5,5], k = 0) == 55

assert Solution().countNonDecreasingSubarrays(nums = [5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5], k = 30) == 120

assert Solution().countNonDecreasingSubarrays(nums = [3, 1, 2, 1, 2, 3, 1, 2, 3, 1], k = 10) == 54

assert Solution().countNonDecreasingSubarrays(nums = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1], k = 20) == 45

assert Solution().countNonDecreasingSubarrays(nums = [5,4,3,2,1,1,1,1,1], k = 10) == 37

assert Solution().countNonDecreasingSubarrays(nums = [5, 4, 3, 2, 1, 2, 3, 4, 5, 6], k = 15) == 52

assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3], k = 10) == 66

assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5], k = 20) == 120

assert Solution().countNonDecreasingSubarrays(nums = [1, 3, 5, 4, 7, 9, 8, 11, 13, 12], k = 20) == 55

assert Solution().countNonDecreasingSubarrays(nums = [1, 3, 2, 4, 3, 5, 4, 6, 5, 7, 6, 8, 7, 9, 8], k = 30) == 120

assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 1, 3, 2, 4, 3, 5, 4, 6], k = 5) == 55

assert Solution().countNonDecreasingSubarrays(nums = [10, 20, 15, 30, 25, 35, 30, 40, 35, 45], k = 30) == 55

assert Solution().countNonDecreasingSubarrays(nums = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], k = 50) == 55

assert Solution().countNonDecreasingSubarrays(nums = [5, 4, 3, 2, 1, 10, 9, 8, 7, 6], k = 20) == 55

assert Solution().countNonDecreasingSubarrays(nums = [10, 20, 10, 20, 10, 20, 10, 20, 10, 20], k = 30) == 51

assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 0) == 55

assert Solution().countNonDecreasingSubarrays(nums = [1,3,2,4,5,6,7,8,9,10], k = 10) == 55

assert Solution().countNonDecreasingSubarrays(nums = [1,2,1,3,2,4,3,5,4,6,5,7], k = 20) == 78

assert Solution().countNonDecreasingSubarrays(nums = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1], k = 25) == 49

Answer: assert Solution().countNonDecreasingSubarrays(nums = [5,4,3,2,1], k = 10) == 15

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10,20,10,20,10], k = 15) == 13

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,2,3,4,5,6,7,8,9,10], k = 1) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,100,2,99,3,98], k = 100) == 17

Answer: assert Solution().countNonDecreasingSubarrays(nums = [100,50,25,12,6,3,1], k = 50) == 17

Answer: assert Solution().countNonDecreasingSubarrays(nums = [6,3,1,2,4,4], k = 7) == 17

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,3,2,4,3,5], k = 5) == 21

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,2,3,4,5], k = 3) == 15

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,2,2,3,4,4,5], k = 6) == 28

Answer: assert Solution().countNonDecreasingSubarrays(nums = [3,2,1,4,5,6], k = 3) == 21

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,10,3,4,2,5], k = 8) == 15

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10,9,8,7,6,5,4,3,2,1], k = 15) == 45

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,2,3], k = 0) == 6

Answer: assert Solution().countNonDecreasingSubarrays(nums = [9,7,5,3,1], k = 5) == 9

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1], k = 1) == 1

Answer: assert Solution().countNonDecreasingSubarrays(nums = [3,2,1,2,3], k = 4) == 15

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10,10,10,10,10], k = 0) == 15

Answer: assert Solution().countNonDecreasingSubarrays(nums = [6,3,1,3,6], k = 4) == 12

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,3,5,2,4,6], k = 5) == 21

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,3,5,7,9], k = 1) == 15

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,3,5,7,9], k = 0) == 15

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,2,2,3,3,3,4], k = 5) == 28

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,1,1,1,1], k = 1) == 15

Answer: assert Solution().countNonDecreasingSubarrays(nums = [9,7,5,3,1], k = 15) == 14

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,1,1,1,1], k = 0) == 15

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10,9,8,7,6,5,4,3,2,1], k = 50) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,3,5,7,9], k = 5) == 15

Answer: assert Solution().countNonDecreasingSubarrays(nums = [7,8,9,10,1,2,3,4,5,6], k = 20) == 39

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10,9,8,7,6,5,4,3,2,1], k = 25) == 49

Answer: assert Solution().countNonDecreasingSubarrays(nums = [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], k = 50) == 105

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1000000000,999999999,999999998,999999997,999999996], k = 1000000000) == 15

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], k = 50) == 120

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], k = 50) == 189

Answer: assert Solution().countNonDecreasingSubarrays(nums = [7, 6, 5, 8, 7, 6, 9, 8, 7, 10, 9, 8, 11, 10, 9], k = 70) == 120

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,3,2,5,4,6,7], k = 5) == 28

Answer: assert Solution().countNonDecreasingSubarrays(nums = [5, 3, 8, 6, 10, 9, 14, 12, 16, 15], k = 50) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 3, 2, 1, 2, 3, 4, 3, 2, 1], k = 15) == 66

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1], k = 45) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], k = 20) == 120

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5], k = 25) == 120

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10, 20, 10, 30, 20, 40, 30, 50, 40, 60], k = 25) == 43

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 5, 3, 7, 8, 2, 6, 4, 9, 10], k = 15) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,100,2,99,3,98,4,97,5,96], k = 500) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [3,1,2,1,3,2,1,3,2,1], k = 30) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10, 20, 10, 20, 10, 20, 10, 20, 10, 20], k = 50) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], k = 0) == 210

Answer: assert Solution().countNonDecreasingSubarrays(nums = [5, 3, 8, 6, 7, 2, 4, 9, 1, 10], k = 20) == 49

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 10, 2, 9, 3, 8, 4, 7, 5, 6], k = 20) == 45

Answer: assert Solution().countNonDecreasingSubarrays(nums = [5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5], k = 1000000) == 9316

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,1,2,2,3,3,4,4,5,5,6,6], k = 15) == 78

Answer: assert Solution().countNonDecreasingSubarrays(nums = [20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], k = 100) == 189

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1], k = 20) == 120

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3], k = 10) == 111

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 3, 2, 4, 3, 5, 4, 6, 5, 7], k = 10) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5], k = 100) == 210

Answer: assert Solution().countNonDecreasingSubarrays(nums = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0], k = 100) == 210

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2], k = 5) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 3, 2, 3, 2, 4, 3, 5, 4, 6], k = 12) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], k = 100) == 120

Answer: assert Solution().countNonDecreasingSubarrays(nums = [5, 3, 1, 6, 4, 2, 7, 5, 3, 8, 6, 4, 9, 7, 5], k = 75) == 120

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1], k = 15) == 120

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1000000000, 999999999, 999999998, 999999997, 999999996, 999999995, 999999994], k = 1000000000) == 28

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29], k = 50) == 120

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10, 20, 30, 25, 35, 40, 30, 45, 50, 40, 55, 60, 50, 65, 70], k = 150) == 120

Answer: assert Solution().countNonDecreasingSubarrays(nums = [5, 5, 5, 5, 5, 5, 5, 5, 5, 5], k = 0) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,1,1,1,1,1,1,1,1,1], k = 5) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [3, 1, 2, 1, 3, 2, 1, 2, 3, 2, 1, 2, 3], k = 15) == 91

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1000000000,999999999,999999998,999999997,999999996,999999995,999999994,999999993,999999992,999999991], k = 4500000000) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 10, 100, 1000, 10000], k = 10000) == 15

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], k = 5) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], k = 100) == 119

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10, 5, 15, 20, 25, 5, 10, 30, 40, 50], k = 50) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [15,14,13,12,11,10,9,8,7,6,5,4,3,2,1], k = 100) == 119

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10], k = 10) == 210

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 3, 2, 4, 3, 5, 4, 6, 5, 7, 6, 8, 7, 9, 8, 10, 9, 11, 10, 12], k = 30) == 210

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 100, 2, 99, 3, 98, 4, 97, 5, 96, 6, 95, 7, 94, 8, 93, 9, 92, 10, 91], k = 500) == 170

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14], k = 50) == 120

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], k = 3) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10,9,8,7,6,5,4,3,2,1], k = 45) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], k = 200) == 120

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,2,1,2,1,2,1,2,1,2,1,2,1,2,1], k = 25) == 120

Answer: assert Solution().countNonDecreasingSubarrays(nums = [100, 50, 100, 50, 100, 50, 100, 50, 100, 50], k = 150) == 50

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10, 1, 10, 1, 10, 1, 10, 1, 10, 1], k = 10) == 30

Answer: assert Solution().countNonDecreasingSubarrays(nums = [3, 3, 3, 3, 3, 3, 3, 3, 3, 3], k = 10) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5], k = 10) == 210

Answer: assert Solution().countNonDecreasingSubarrays(nums = [5, 4, 3, 2, 1, 0, -1, -2, -3, -4], k = 30) == 52

Answer: assert Solution().countNonDecreasingSubarrays(nums = [5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1], k = 50) == 120

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,1,1,1,1,1,1,1,1,1], k = 0) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [5,10,15,20,25,30,35,40,45,50], k = 100) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], k = 20) == 105

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,2,3,2,1,2,3,4,5,6], k = 15) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 100, 2, 99, 3, 98, 4, 97, 5, 96], k = 150) == 31

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 10, 2, 9, 3, 8, 4, 7, 5, 6], k = 25) == 49

Answer: assert Solution().countNonDecreasingSubarrays(nums = [3,3,3,3,3,3,3,3,3,3], k = 100) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [5,4,5,4,5,4,5,4,5,4], k = 20) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 10, 2, 9, 3, 8, 4, 7, 5, 6, 1, 10, 2, 9, 3, 8, 4, 7, 5, 6], k = 60) == 194

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,10,20,30,40,50,60,70,80,90,100,90,80,70,60,50,40,30,20,10,1], k = 1000) == 231

Answer: assert Solution().countNonDecreasingSubarrays(nums = [20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], k = 190) == 210

Answer: assert Solution().countNonDecreasingSubarrays(nums = [5,5,5,5,5,5,5,5,5,5], k = 0) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1000000000, 1000000000, 1000000000, 1000000000, 1000000000], k = 1000000000) == 15

Answer: assert Solution().countNonDecreasingSubarrays(nums = [5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5], k = 30) == 120

Answer: assert Solution().countNonDecreasingSubarrays(nums = [3, 1, 2, 1, 2, 3, 1, 2, 3, 1], k = 10) == 54

Answer: assert Solution().countNonDecreasingSubarrays(nums = [5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], k = 40) == 210

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10,9,8,7,6,5,4,3,2,1,10,9,8,7,6], k = 50) == 118

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150], k = 150) == 120

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1], k = 20) == 45

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], k = 100) == 120

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], k = 50) == 210

Answer: assert Solution().countNonDecreasingSubarrays(nums = [5,4,3,2,1,1,1,1,1], k = 10) == 37

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], k = 100) == 210

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10, 20, 10, 20, 10, 20, 10, 20, 10, 20, 10, 20, 10, 20, 10], k = 50) == 112

Answer: assert Solution().countNonDecreasingSubarrays(nums = [3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], k = 20) == 231

Answer: assert Solution().countNonDecreasingSubarrays(nums = [5, 4, 3, 2, 1, 2, 3, 4, 5, 6], k = 15) == 52

Answer: assert Solution().countNonDecreasingSubarrays(nums = [9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 9, 8, 7, 6, 5], k = 50) == 119

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3], k = 10) == 66

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5], k = 20) == 120

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], k = 0) == 210

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 3, 5, 4, 7, 9, 8, 11, 13, 12], k = 20) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 3, 2, 4, 3, 5, 4, 6, 5, 7, 6, 8, 7, 9, 8], k = 30) == 120

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2], k = 15) == 210

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,2,2,1,2,3,4,5,5,4,3,2,1,2,3,4,5], k = 20) == 153

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 1, 3, 2, 4, 3, 5, 4, 6], k = 5) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [150, 140, 130, 120, 110, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10], k = 200) == 75

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10, 20, 15, 30, 25, 35, 30, 40, 35, 45], k = 30) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], k = 10) == 120

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], k = 50) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [5, 4, 3, 2, 1, 10, 9, 8, 7, 6], k = 20) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10, 20, 10, 20, 10, 20, 10, 20, 10, 20], k = 30) == 51

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 0) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], k = 100) == 210

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,3,2,4,5,6,7,8,9,10], k = 10) == 55

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200], k = 1000) == 210

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1,2,1,3,2,4,3,5,4,6,5,7], k = 20) == 78

Answer: assert Solution().countNonDecreasingSubarrays(nums = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5], k = 50) == 210

Answer: assert Solution().countNonDecreasingSubarrays(nums = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1], k = 25) == 49

import random
import functools
import collections
import string
import math
import datetime

from typing import *
from functools import *
from collections import *
from itertools import *
from heapq import *
from bisect import *
from string import *
from operator import *
from math import *

inf = float('inf')

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def list_node(values: list):
    if not values:
        return None
    head = ListNode(values[0])
    p = head
    for val in values[1:]:
        node = ListNode(val)
        p.next = node
        p = node
    return head

def is_same_list(p1, p2):
    if p1 is None and p2 is None:
        return True
    if not p1 or not p2:
        return False
    return p1.val == p2.val and is_same_list(p1.next, p2.next)

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def tree_node(values: list):
    if not values:
        return None
    root = TreeNode(values[0])
    i = 1
    queue = deque()
    queue.append(root)
    while queue:
        node = queue.popleft()
        if i < len(values) and values[i] is not None:
            node.left = TreeNode(values[i])
            queue.append(node.left)
        i += 1
        if i < len(values) and values[i] is not None:
            node.right = TreeNode(values[i])
            queue.append(node.right)
        i += 1
    return root

def is_same_tree(p, q):
    if not p and not q:
        return True
    elif not p or not q:
        return False
    elif p.val != q.val:
        return False
    else:
        return is_same_tree(p.left, q.left) and is_same_tree(p.right, q.right)

class Solution:
  def countNonDecreasingSubarrays(self, nums: list[int], k: int) -> int:
    ans = 0
    cost = 0
    # Store (number, count) pairs in non-increasing order. The numbers in the
    # queue represent what nums[i..j] look like after adjustments.
    dq = collections.deque()

    j = len(nums) - 1
    for i, num in reversed(list(enumerate(nums))):
      count = 1
      while dq and dq[-1][0] < num:
        nextNum, nextCount = dq.pop()
        count += nextCount
        cost += (num - nextNum) * nextCount  # Adjust `nextNum`s to `num`.
      dq.append((num, count))
      while cost > k:  # Remove the rightmost number.
        rightmostNum, rightmostCount = dq.popleft()
        cost -= (rightmostNum - nums[j])
        j -= 1
        if rightmostCount > 1:
          dq.appendleft((rightmostNum, rightmostCount - 1))
      ans += j - i + 1

    return ans

class Solution:
    def countNonDecreasingSubarrays(self, nums: List[int], k: int) -> int: