80 Days is an interesting game based on Jules Verne’s science fiction “Around the World in Eighty Days”. In this game, you have to manage the limited money and time.
Now we simplified the game as below:
There are n cities on a circle around the world which are numbered from 1 to n by their order on the circle. When you reach the city i at the first time, you will get ai dollars (ai can even be negative), and if you want to go to the next city on the circle, you should pay bi dollars. At the beginning you have c dollars.
The goal of this game is to choose a city as start point, then go along the circle and visit all the city once, and finally return to the start point. During the trip, the money you have must be no less than zero.
Here comes a question: to complete the trip, which city will you choose to be the start city?
If there are multiple answers, please output the one with the smallest number.
Input
The first line of the input is an integer T (T ≤ 100), the number of test cases.
For each test case, the first line contains two integers n and c (1 ≤ n ≤ 106, 0 ≤ c ≤ 109). The second line contains n integers a1, …, an (-109 ≤ ai ≤ 109), and the third line contains n integers b1, …, bn (0 ≤ bi ≤ 109).
It’s guaranteed that the sum of n of all test cases is less than 106
Output
For each test case, output the start city you should choose.
Sample Input
2
3 0
3 4 5
5 4 3
3 100
-3 -4 -5
30 40 50
Sample Output
2
-1
Hint
For test case 1, both city 2 and 3 could be chosen as start point, 2 has smaller number. But if you start at city 1, you can’t go anywhere.
For test case 2, start from which city seems doesn’t matter, you just don’t have enough money to complete a trip.
**The main idea of the topic: ** Given n cities, their numbers are 1~n, and they are connected end to end to form a ring. Every time you pass through a city, you will get ai money, and every time you leave a city, you will spend bi Money, it is required to choose a city as the starting point, then go through n cities clockwise, and ensure that the amount of money in the journey cannot be less than 0
**Question analysis: **This question is because it said that the total n is not greater than 1e6, it can be completely violent, one level of enumeration starting point, one level of prefix and judgment whether the conditions are met, if the conditions are met, the serial number is output, if If the conditions are not met, continue to search. Each point is traversed to see if the conditions are met. Although it looks like the complexity of n*n, it will actually be much smaller with pruning.
AC code:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<iomanip>
#include<map>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<set>
#include<cctype>
#include<string>
#include<stdexcept>
#include<fstream>
#define mem(a,b) memset(a,b,sizeof(a))
#define debug() puts("what the fuck!")
#define debug(a) cout<<#a<<"="<<a<<endl;
#define speed {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); };
#define ll long long
#define mod 998244353
using namespace std;
const double PI = acos(-1.0);
const int maxn = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const double esp_0 = 1e-6;
ll gcd(ll x, ll y) {
return y ? gcd(y, x % y) : x;
}
ll lcm(ll x, ll y) {
return x * y / gcd(x, y);
}
ll extends_gcd(ll a, ll b, ll& x, ll& y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll gcdd = extends_gcd(b, a % b, x, y);
ll temp = x;
x = y;
y = temp - (a / b) * y;
return gcdd;
}
int n, c;
int a[maxn];
int judge(int x) {
ll sum = c;
for (int i = 1; i <= n; ++i) {
sum += a[x];
if (sum < 0)return 0;
x++;
if (x == n + 1)x = 1;
}
return 1;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &c);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
ll sum = c;
for (int i = 1; i <= n; ++i) {
int cost;
scanf("%d", &cost);
a[i] -= cost;
sum += a[i];
}
if (sum < 0) {
printf("-1\n");
continue;
}
else {
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (judge(i)) {
ans = i;
break;
}
}
printf("%d\n", ans);
}
}
return 0;
}