
Sinbad is traveling along a path of N points, numbered 0 to N-1. He starts at point 0 with an initial energy E and aims to reach the final point N-1.
Along the path, there are two factors that affect his journey:
On each day, Sinbad is at a point i and attempts to move to i+1.
Your task is to find the maximum energy Sinbad can have upon arriving at point N-1. If he can never reach the final point, return -1.
The first line contains two space-separated integers: N and E (initial energy).
The second line contains N space-separated integers for the energy array.
The third line contains N space-separated integers for the wind array.
Print a single integer representing the maximum energy at the destination.
If the destination is unreachable, print -1.
The journey is a linear simulation from left to right.
3 5
0 0 0
0 -10 0
-1
1. Start at point 0: Initial energy E=5. Gains `energy[0]=0`. Current energy = 5.
2. Move 0 -> 1: `wind[0]=0`. Cost = 1. Energy `5 >= 1`.
- Pays cost: `5 - 1 = 4`.
- Arrives at point 1, gains `energy[1]=0`. Current energy = 4.
3. Move 1 -> 2: `wind[1]=-10`. Cost = `1 + abs(-10) = 11`.
- Sinbad's energy (4) is less than the cost (11). He cannot move.
The journey is impossible.
4 10
0 5 0 10
0 -2 1 0
21
1. Start at point 0: Initial energy E=10. Gains `energy[0]=0`. Current energy = 10.
2. Move 0 -> 1: `wind[0]=0`. Cost = 1. Energy `10 >= 1`.
- Pays cost: `10 - 1 = 9`.
- Arrives at point 1, gains `energy[1]=5`. Current energy = `9 + 5 = 14`.
3. Move 1 -> 2: `wind[1]=-2`. Cost = `1 + abs(-2) = 3`. Energy `14 >= 3`.
- Pays cost: `14 - 3 = 11`.
- Arrives at point 2, gains `energy[2]=0`. Current energy = `11 + 0 = 11`.
4. Move 2 -> 3: `wind[2]=1`. Cost = `0`. Energy `11 >= 0`.
- Pays cost: `11 - 0 = 11`.
- Arrives at point 3, gains `energy[3]=10`. Final energy = `11 + 10 = 21`.
The expected time complexity is O(N).
2 <= N <= 10^5
0 <= E <= 10^9
0 <= energy[i] <= 1000
-1000 <= wind[i] <= 1000
Time limit: 1 sec