Polycarp started working at a bank. He was assigned to monitor the ATM. The ATM initially contains s rubles.
A queue of n students lined up to him. Each student wants to either withdraw a certain amount of money or deposit it into an account. If a i a_i ai is positive, then the student credits that amount of money via ATM. Otherwise, the student withdraws ∣ a i ∣ |a_i| ∣ai∣ rubles.
In the beginning, the ATM is turned off and an arbitrary number of students are not served. At some point, Polycarp turns on the ATM, which has an initial amount of s rubles. Then, the remaining students start queueing at the ATM. If at some point in time there is less money in the ATM than the student wants to withdraw, then the student is not served and Polycarp turns off the ATM and does not turn it on anymore.
More formally, the students that are served are forming a contiguous subsequence.
Polycarp wants the ATM to serve the maximum number of students. Help him in this matter. Print the numbers of the first and last student, or determine that he will not be able to serve anyone.
In other words, find such a longest continuous segment of students that, starting with the sum of s s s at the ATM, all these students will be served. ATM serves students consistently (i.e. one after another in the order of the queue).
Input
The first line of the input contains one integer t ( 1 ≤ t ≤ 1 0 4 ) t (1≤t≤10^4) t(1≤t≤104) — the number of test cases.
Each test case consists of two lines. The first one contains integers n n n and s s s ( 1 ≤ n ≤ 2 ⋅ 1 0 5 ; 0 ≤ s ≤ 1 0 9 ) (1≤n≤2⋅10^5; 0≤s≤10^9) (1≤n≤2⋅105;0≤s≤109) — the length of the a array and the initial amount of rubles in the ATM. The second contains n integers a 1 a_1 a1, a 2 a_2 a2,…, a n a_n an ( − 1 0 9 ≤ a i ≤ 1 0 9 ) (−10^9≤a_i≤10^9) (−109≤ai≤109) — elements of the a array. Note that a i a_i ai can be zero.
It is guaranteed that the sum of the values n n n over all test cases does not exceed 2 ⋅ 1 0 5 2⋅10^5 2⋅105.
Output
Print t lines, each line must contain the answer to the corresponding set of input data: if the answer exists, print the numbers of the first and last served student. If there is no solution, then print -1 on the line.
If there are several possible answers, print any.
Example:
input:3 4 10 -16 2 -6 8 3 1000 -100000 -100000 -100000 6 0 2 6 -164 1 -1 -6543
output:
2 4 -1 1 2
Note:
In the first test case, the only correct answer is 2 4, since when serving students, the number of rubles at the ATM does not become negative, and this is the maximum number of students that can be served.
In the second test case, the answer is -1, as there is not enough money for any student at the ATM.
In the third test case, the answer can be either 1 2 or 4 5.
解题思路: 利用双指针跑,左右指针同一出发点,如果当前连续子列和加上右指针指向的数能够 ≥ − s ≥-s ≥−s 的,右指针就往后移,直到不满足条件。就这样找到当前符合要求的最右的指针,判断当前右指针和左指针的差是否大于记录的左右指针差(我一开始都为0),如果大于就更新记录的左右指针。然后将连续子列和减去左指针指向的数,并往后移。直到左或右指针跑完就算结束。
(如果右指针未移动,左指针也会后移,再次进入循环后,右指针就能移动了。因为右指针不符合的条件,左指针后移后再进入循环,就能够抵消)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int t; cin>>t;
while(t--)
{
ll n, s; cin>>n>>s;
vector<ll> a(n+1);
for(int i=1; i<=n; i++) cin>>a[i];
int l=0, r=0, i=1, j=1;
ll sum = 0;
while(i<=n && j<=n)
{
while(sum+a[j]+s>=0 && j<=n) sum += a[j++];
if(j-i > r-l)
{
l = i;
r = j;
}
sum -= a[i++];
}
if(l && r)
cout<<l<<' '<<r-1<<'\n';
else cout<<"-1\n";
}
return 0;
}