#K88212. Taco Trip
Taco Trip
Taco Trip
Sophia has planned a multi‐day trip, and she wants to know whether her planned stops will allow her to complete each segment of the journey without exceeding a specified distance limit. In each test case, you are given the total number of days in the trip, a list of daily distances, a list of stops (represented by day indices) where she plans to take a break, and a distance limit. You must determine if the sum of distances between every two consecutive stops (including the start at day 0 and the end at day n) never exceeds the given limit. Formally, let \(n\) be the total number of days, and let the stops be given by a list \(S\). Consider the extended list \(S' = [0] + S + [n]\). For every consecutive pair \(S'_i\) and \(S'_{i+1}\), if \(\sum_{j=S'_i}^{S'_{i+1}-1} d_j \leq L\) holds (where \(d_j\) are the daily distances and \(L\) is the limit), then the trip is possible; otherwise, it is impossible.
Your task is to implement a solution that processes multiple test cases and prints "POSSIBLE" for those that meet the requirement or "IMPOSSIBLE" otherwise.
inputFormat
The first line of input contains an integer \(T\) (the number of test cases). Each test case is described as follows:
- A line containing an integer \(n\) (the number of days in the trip).
- A line containing \(n\) space-separated integers representing the daily distances.
- A line containing an integer \(k\), the number of planned stops.
- If \(k > 0\), a line with \(k\) space-separated integers representing the day indices (1-indexed) where stops occur. If \(k = 0\), this line will be empty.
- A line containing an integer \(L\), the maximum allowed distance for any segment between stops.
Note that the stops are used to partition the trip. The start (day 0) and end (day \(n\)) are considered as implicit stops.
outputFormat
For each test case, output a single line containing either "POSSIBLE" or "IMPOSSIBLE" depending on whether every segment (between consecutive stops including the start and end) has a total distance less than or equal to \(L\).
## sample5
4
300 400 200 500
2
2 4
1000
5
100 200 300 400 500
3
1 3 5
800
1
1000
0
10000
2
500 500
1
1
1000
3
100 100 100
0
300
POSSIBLE
IMPOSSIBLE
POSSIBLE
POSSIBLE
POSSIBLE
</p>