The only difference between the easy and hard versions are the locations you can teleport to.

Consider the points $0,1,\dots,n+1$ on the number line. There is a teleporter located on each of the points $1,2,\dots,n$. At point $i$, you can do the following:

Move left one unit: it costs $1$ coin.

Move right one unit: it costs $1$ coin.

Use a teleporter at point $i$, if it exists: it costs $a_i$ coins. As a result, you can choose whether to teleport to point $0$ or point $n+1$. Once you use a teleporter, you can't use it again.

You have $c$ coins, and you start at point $0$. What's the most number of teleporters you can use?


-----Input-----

The input consists of multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The descriptions of the test cases follow.

The first line of each test case contains two integers $n$ and $c$ ($1 \leq n \leq 2\cdot10^5$; $1 \leq c \leq 10^9$)  — the length of the array and the number of coins you have respectively.

The following line contains $n$ space-separated positive integers $a_1,a_2,\dots,a_n$ ($1 \leq a_i \leq 10^9$) — the costs to use the teleporters.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot10^5$.


-----Output-----

For each test case, output the maximum number of teleporters you can use.


-----Examples-----

Input
10
5 6
1 1 1 1 1
8 32
100 52 13 6 9 4 100 35
1 1
5
4 5
4 3 2 1
5 9
2 3 1 4 1
5 8
2 3 1 4 1
4 3
2 3 4 1
4 9
5 4 3 3
2 14
7 5
5 600000000
500000000 400000000 300000000 200000000 100000000
Output
2
3
0
1
3
2
1
1
2
2


-----Note-----

In the first test case, you can move one unit to the right, use the teleporter at index $1$ and teleport to point $n+1$, move one unit to the left and use the teleporter at index $5$. You are left with $6-1-1-1-1 = 2$ coins, and wherever you teleport, you won't have enough coins to use another teleporter. You have used two teleporters, so the answer is two.

In the second test case, you go four units to the right and use the teleporter to go to $n+1$, then go three units left and use the teleporter at index $6$ to go to $n+1$, and finally, you go left four times and use the teleporter. The total cost will be $4+6+3+4+4+9 = 30$, and you used three teleporters.

In the third test case, you don't have enough coins to use any teleporter, so the answer is zero.

Difficulty: HARD
Tags: Sorting

Greedy algorithms
Skill types: Sorting

Greedy algorithms

Starter code:

Solutions:
import bisect

def f(arr, c):
	n = len(arr)
	total = 0
	start = []
	for i in range(n):
		start.append(arr[i] + (i + 1))
		arr[i] += min(i + 1, n - i)
	sorted_arr = sorted(arr)
	pref = [sorted_arr[0]]
	for i in range(1, n):
		pref.append(pref[-1] + sorted_arr[i])
	for i in range(n):
		if c - start[i] >= 0:
			index = bisect.bisect_left(pref, c - start[i])
			if index >= n:
				index -= 1
			if pref[index] > c - start[i]:
				index -= 1
			if index == -1:
				total = max(total, 1)
				continue
			if sorted_arr[index] >= arr[i]:
				index = index = bisect.bisect_left(pref, c - (start[i] - arr[i]))
				if index >= n:
					index -= 1
				if pref[index] > c - (start[i] - arr[i]):
					index -= 1
				cur = index + 1
			else:
				cur = index + 2
			total = max(total, cur)
	return total
k = int(input())
res = []
while k > 0:
	nc = input().split(' ')
	(n, c) = (int(nc[0]), int(nc[1]))
	temp = input().split(' ')
	arr = []
	for num in temp:
		arr.append(int(num))
	res.append(f(arr, c))
	k -= 1
for x in res:
	print(x)


from bisect import bisect
t = int(input())
for _ in range(t):
	(n, c) = map(int, input().split())
	a = list(map(int, input().split()))
	left_cost = [num + index + 1 for (index, num) in enumerate(a)]
	costs = [(num + min(index + 1, n - index), index) for (index, num) in enumerate(a)]
	costs.sort()
	total_costs = [0] * n
	total_costs[0] = costs[0][0]
	pos = [0] * n
	pos[costs[0][1]] = 0
	for i in range(1, n):
		pos[costs[i][1]] = i
		total_costs[i] = total_costs[i - 1] + costs[i][0]
	max_used = 0
	for (index, cost) in enumerate(left_cost):
		if cost > c:
			continue
		used = bisect(total_costs, c - cost)
		if pos[index] < used:
			if used < n and total_costs[used] - costs[pos[index]][0] <= c - cost:
				used += 1
			used -= 1
		max_used = max(max_used, used + 1)
	print(max_used)