After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you).
You are given an array h_1, h_2, ..., h_n, where h_i is the height of the i-th mountain, and k — the number of boulders you have.
You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is h_i):
if h_i ≥ h_{i + 1}, the boulder will roll to the next mountain;
if h_i < h_{i + 1}, the boulder will stop rolling and increase the mountain height by 1 (h_i = h_i + 1);
if the boulder reaches the last mountain it will fall to the waste collection system and disappear.
You want to find the position of the k-th boulder or determine that it will fall into the waste collection system.
Input
The first line contains a single integer t (1 ≤ t ≤ 100) — the number of test cases.
Each test case consists of two lines. The first line in each test case contains two integers n and k (1 ≤ n ≤ 100; 1 ≤ k ≤ 10^9) — the number of mountains and the number of boulders.
The second line contains n integers h_1, h_2, ..., h_n (1 ≤ h_i ≤ 100) — the height of the mountains.
It is guaranteed that the sum of n over all test cases does not exceed 100.
Output
For each test case, print -1 if the k-th boulder will fall into the collection system. Otherwise, print the position of the k-th boulder.
Example
Input
4
4 3
4 1 2 3
2 7
1 8
4 5
4 1 2 3
3 1
5 3 1
Output
2
1
-1
-1
Note
Let's simulate the first case:
The first boulder starts at i = 1; since h_1 ≥ h_2 it rolls to i = 2 and stops there because h_2 < h_3.
The new heights are [4,2,2,3].
The second boulder starts at i = 1; since h_1 ≥ h_2 the boulder rolls to i = 2; since h_2 ≥ h_3 the boulder rolls to i = 3 and stops there because h_3 < h_4.
The new heights are [4,2,3,3].
The third boulder starts at i = 1; since h_1 ≥ h_2 it rolls to i = 2 and stops there because h_2 < h_3.
The new heights are [4,3,3,3].
The positions where each boulder stopped are the following: [2,3,2].
In the second case, all 7 boulders will stop right at the first mountain rising its height from 1 to 8.
The third case is similar to the first one but now you'll throw 5 boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to [4, 3, 3, 3], that's why the other two boulders will fall into the collection system.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
In the fourth case, the first and only boulders will fall straight into the collection system.
We need to simulate the process of rolling boulders up to k boulders, but constraints: n ≤ 100 per test case, sum n ≤ 100, but k up to 1e9. So we cannot simulate each boulder if many are thrown. We need to find the position of the k-th boulder efficiently.
Given the process: Starting from the first mountain, a boulder rolls left to right? Actually they start from the top of the first mountain (i=1). They check: if current mountain's height >= next mountain's height, it rolls to next. If current height < next height, it stops and increases the current mountain's height by 1. And if it reaches last mountain (i = n), it falls off (disappears). Actually "if the boulder reaches the last mountain it will fall to the waste collection system and disappear." That means: When a boulder is at the last mountain (i = n), does it fall? The description: "if the boulder reaches the last mountain it will fall to the waste collection system and disappear." So if after rolling we are at i=n, then it falls. But what about if at i=n-1, check h_{n-1} >= h_n? If it is >=, then it rolls to n, and then falls. So essentially, a boulder will fall if it passes through all mountains without stopping earlier.
Given that heights are up to 100 initially, and we increment heights by 1 at stopping points. After many boulders, heights may increase. However, due to the rule, boulders only increase the height of a mountain where they stop. This tends to make the heights non-decreasing? Actually the process stops when current mountain's height is less than the next mountain's height. That condition: h_i < h_{i+1}. So a boulder stops at the first i (starting from 1 and moving forward) such that h_i < h_{i+1} (or i = n? But if it reaches last mountain, it falls; but the rule says: if the boulder reaches the last mountain it will fall. So the boulder starts at mountain 1? Actually "start throwing boulders from the top of the first mountain" suggests the boulder is initially at mountain 1? Or do we start at the first mountain and then apply rule? The description: "if h_i ≥ h_{i+1}, the boulder will roll to the next mountain; if h_i < h_{i+1}, the boulder will stop rolling and increase the mountain height by 1 (h_i = h_i + 1)". So we consider the current mountain i. The boulder is at some mountain; we check the condition relative to next mountain. So it moves from i to i+1 only if h_i >= h_{i+1}. If it moves, then at the new i, repeat. So the boulder will eventually stop at some i where h_i < h_{i+1} (i < n). Or if it reaches i = n (i.e., after moving from n-1 to n, then we are at the last mountain? Actually after moving to n, there is no next mountain; the boulder falls. But the rule "if the boulder reaches the last mountain it will fall" suggests that when the boulder arrives at i = n (i.e., after a move, or maybe initially if n=1? But n >= 1). So when the boulder is at i = n, it falls and no height change. Also if at i = n-1 and h_{n-1} < h_n, it would stop at n-1 (increase). So the only way a boulder falls is if for all i from 1 to n-1, we have h_i >= h_{i+1}, i.e., the heights are non-increasing. Because then it will keep rolling until i=n, then fall. So a boulder falls iff the sequence is non-increasing (strictly? Actually h_i >= h_{i+1} for all i from 1 to n-1). If there's any descent (h_i < h_{i+1}) early, it stops at the first such i.
Thus, as we throw boulders, heights change: each time a boulder stops at some position i (1 <= i < n), it increases h_i by 1. This increase might affect future boulders: it may make that position become >= the next mountain? Possibly after enough increments, the condition h_i >= h_{i+1} might become true, and then boulders will roll past it to later positions. Over time, the boulders can "fill" the valleys, making the heights more non-increasing.
We need to find the stopping position of the k-th boulder, or -1 if it falls.
Given n <= 100, k up to 1e9, but sum n <= 100. So we can simulate the process until either we have thrown k boulders, or we detect that further boulders will always fall (i.e., the heights become non-increasing), or that the process becomes periodic? Since heights only increase, and each boulder increases exactly one h_i by 1. The total heights sum increases by 1 per boulder until it falls. If it falls, then no height change. So if at some point the configuration becomes non-increasing, all subsequent boulders will fall (since no changes occur? Actually if it falls, heights remain unchanged. So once it's non-increasing, all further boulders will also fall). So we can stop simulation when that happens.
But can we have a situation where the process never becomes fully non-increasing? Let's think: We start with heights up to 100, n up to 100. Over many boulders, heights increase, but eventually it's possible that the heights become strictly increasing? Actually we need non-increasing for falls. Could the process oscillate? No, because only increments happen at positions where h_i < h_{i+1}. This increment will reduce the gap between h_i and h_{i+1} (since h_i increases by 1). Eventually, for each i where initially h_i < h_{i+1}, after enough increments at i, we may get h_i >= h_{i+1}. At that point, that particular "ascending" is eliminated. But after that, when h_i >= h_{i+1}, boulders may start to roll past i to later positions. But those later positions might still have ascents (h_j < h_{j+1}). Over time, as boulders stop at the first ascent, they will increment the earliest position where h_i < h_{i+1}. This is reminiscent of the "pouring water" or "ball dropping" problems, where each ball goes to the leftmost gap? Actually here it's like: Starting at position 1, we find the first i (1 <= i < n) such that h_i < h_{i+1}. Then increment h_i. This is exactly like the process of adding grains of sand to make a non-decreasing? Wait, it's about making the sequence non-increasing? Because we stop when h_i < h_{i+1} (i.e., an increase). So we increment the leftmost position where an increase occurs (i.e., where height goes up from i to i+1). By incrementing that leftmost "increase", we may eventually flatten it. This process is similar to the algorithm for adding pebbles to the first position where height is less than next? Actually it's like we're trying to make the sequence non-increasing? Let's examine: The condition for boulder to roll is h_i >= h_{i+1}, so the boulder will go through any non-increasing segment. It stops at the first i where h_i < h_{i+1} (i.e., a rise). So the boulder essentially identifies the first "violation" of non-increasing order and raises the left side of that violation. Over many boulders, we are essentially building a non-increasing sequence? The boulder increases h_i where there is an ascent, thus making it larger, moving toward making it >= h_{i+1}. So the process tends to make the sequence more non-increasing. Eventually, if we add enough boulders, the sequence could become non-increasing (i.e., h_i >= h_{i+1} for all i). Once that happens, all future boulders fall. So indeed after a finite number of boulders, we will either have thrown k boulders before that point, or after some point they all fall. Since n and heights are bounded? Actually heights can increase without bound as we keep throwing boulders? But if the sequence becomes non-increasing, boulders fall and we stop increments. So heights are only incremented until non-increasing state is reached. The number of increments needed to reach that state is finite because each increment reduces the number of inversions? Let's compute: The number of "ascents" (i where h_i < h_{i+1}) may be reduced. But incrementing h_i could potentially create a new ascent earlier? No, because incrementing h_i only affects the pair (i-1, i) and (i, i+1). The pair (i-1, i): originally we had h_{i-1} >= h_i? Actually we stopped at i because h_i < h_{i+1}. However, we don't know about h_{i-1} relative to h_i. Since we rolled to i, that means for all j < i, we had h_j >= h_{j+1}. So for i-1, we had h_{i-1} >= h_i. After incrementing h_i, the condition h_{i-1} >= h_i may become false (if h_{i-1} was exactly equal to h_i, then after increment, h_i becomes larger, so h_{i-1} might become < h_i). That would create a new ascent at i-1? Actually the condition for an ascent is h_{i-1} < h_i. So if originally h_{i-1} >= h_i, after incrementing h_i, it could become h_{i-1} < h_i, thus creating a new ascent at i-1. That is crucial: The process may cause earlier positions to become ascents, which then become the first ascent for subsequent boulders. So this is like a "chip-firing" or "water flow" process that can propagate backwards. This is reminiscent of the problem "Balls and Sticks" or "Filling holes" where adding a unit to a position might cause it to overflow to the left? Actually here the boulder always goes right until it finds an ascent, then increments left side. The increment may cause a new ascent earlier, so next boulder may stop earlier. Over time, the process may result in a "staircase" that gradually levels out.
We need to simulate up to k boulders, but k can be huge. However, n is small (<=100), and initial heights <=100. The process might continue for a large number of boulders before becoming non-increasing? Let's analyze the potential maximum number of boulders before the sequence becomes non-increasing.
Suppose we have an array of length n. The process: Each boulder stops at the smallest i such that h_i < h_{i+1} (if any). Then increment h_i by 1. This is essentially like repeatedly adding 1 to the first position where the condition fails (like performing a greedy algorithm to turn the sequence into a non-increasing one). This process is similar to the "incremental sorting" or "balancing" algorithm. Could the number of steps be huge? Possibly yes, because heights can keep increasing, but the boulder may fall only after the whole array is non-increasing. However, the non-increasing condition is h_i >= h_{i+1} for all i. Starting from heights up to 100, we can keep adding boulders to raise some positions; but note that raising a mountain may create earlier ascents, leading to boulders stopping earlier, and eventually the whole array becomes non-increasing? Let's think about what the eventual state might be. Since we only increase heights, the array's values can grow arbitrarily large if we keep throwing boulders? But if the array becomes non-increasing, then boulders fall and no more increments. So the process stops at some finite state? Actually we could keep throwing boulders even after they fall; they would not change heights. So the process will reach a state where it's non-increasing after a finite number of increments. How many increments are needed? Could it be infinite? No, because each increment increases the sum of heights by 1, but is there an upper bound on the sum such that the array must become non-increasing? Not necessarily; we could have a scenario where we keep adding boulders and they never fall? That would require that the array never becomes non-increasing; but each increment increases a particular h_i. Could it be that the process never reaches a non-increasing state? Consider n=2. The process: we have two mountains. A boulder starts at i=1. If h1 < h2, it stops at i=1 and increments h1. If h1 >= h2, it rolls to i=2 (last) and falls. So for n=2, after some number of increments, h1 will become >= h2 (since each increment increases h1 when h1<h2). Once h1 >= h2, further boulders fall. So it will eventually become non-increasing after at most (h2 - h1) increments if initially h1 < h2. So it's finite.
For n>2, it might be more complex. But we can think in terms of a known problem: "Rolling boulders" is similar to Codeforces problem "Falling Boulders" perhaps? I recall a Codeforces problem: "Boulder" or "Rolling boulder" where we need to find position after k boulders. The typical solution: simulate until either k boulders are thrown or we detect that after some steps it becomes stable (non-increasing). Because n <= 100 and k up to 1e9, but we cannot simulate all k if k is huge. However, note that the number of possible configurations (state space) is not huge? Each h_i can increase potentially unbounded, so infinite states. But maybe the process cycles? It shouldn't cycle because heights only increase. So each state is unique and moves towards a non-increasing state. Could the number of steps before reaching non-increasing be huge? Let's try to estimate worst-case scenario.
Consider n=100, heights initially all 1 (so non-decreasing? Actually 1,1,1,... is non-increasing (since equal) so it's non-increasing? Non-increasing means each element >= next; equal is okay. So if all equal, then h_i >= h_{i+1} because equal, so it's non-increasing. Then all boulders fall. So we need a configuration that requires many boulders to become non-increasing. For a given n, what is the maximum number of boulders that can be thrown before it becomes non-increasing? This is essentially the number of increments needed to "fix" all ascents.
Let’s define an array of heights. The process resembles a variant of "adding one to the first positive slope". This process may require many steps. Could it be on the order of O(n * max_height)? Possibly exponential? But we need to handle k up to 1e9. Might there be cases where we need more than 1e9 steps? Possibly yes? Let's explore. Since n <= 100 and initial heights <= 100, but we could theoretically have many steps if the process doesn't converge quickly. However, maybe there is an upper bound on the number of steps before it becomes non-increasing, and that bound might be less than something like n * max_h maybe 100*100=10000? Let's test small cases manually to get intuition.
Take n=3. Let heights = [1, 100, 1]. Initially: h1=1, h2=100, h3=1. The first ascent? Start from i=1: h1 < h2, so stop at i=1, increment h1 => [2,100,1]. Next boulder: again at i=1, check h1=2 < h2=100, stop at 1 => [3,100,1]. Continue until h1 >= h2? That requires h1 >= 100, so 99 increments. So after 99 boulders, h1=100, h2=100, h3=1. Now heights: [100,100,1]. At this point, is it non-increasing? Check: h1=100 >= h2=100 yes; h2=100 >= h3=1 yes. So it is non-increasing? Actually 100>=100 and 100>=1, so yes non-increasing. But wait: The condition for boulder to stop is h_i < h_{i+1}. So if it's non-increasing, boulders will roll through and fall. After 99 boulders, we have non-increasing? Check: h1=100, h2=100, h3=1. That is non-increasing (since 100>=100>=1). So the 100th boulder would fall. So total boulders that stop before falling = 99. So that's fine.
But is there a scenario where after fixing the first ascent, a new ascent emerges earlier, causing many more steps? For example, suppose we have [1, 2, 3] initially. Let's simulate:
Start: [1,2,3]. First boulder: i=1: 1<2 => stop at 1 => [2,2,3].
Next: start at 1: h1=2, h2=2: since h1>=h2, rolls to i=2. At i=2: h2=2 < h3=3 => stop at i=2 => [2,3,3]? Actually increment h2: [2,3,3].
Next: start at 1: h1=2, h2=3: h1< h2, stop at i=1 => [3,3,3].
Next: now heights [3,3,3] which is non-increasing (3>=3>=3). So boulders fall. So steps: 3 boulders stopped.
Now consider [1, 2, 2, 3] maybe? Let's simulate quickly:
Initial: [1,2,2,3]
B1: stop at 1 => [2,2,2,3]
B2: start: h1=2, h2=2: roll to i=2; at i=2: h2=2, h3=2: roll to i=3; at i=3: h3=2 < h4=3 => stop at i=3 => [2,2,3,3]
B3: start: h1=2, h2=2: roll to i=2; at i=2: h2=2, h3=3: h2< h3? Actually 2<3 so stop at i=2 => [2,3,3,3]
B4: start: h1=2, h2=3: stop at i=1 => [3,3,3,3] -> non-increasing. So 4 steps.
It seems that the number of steps required is bounded by something like sum over all ascents of the differences? Possibly the total number of increments equals the total "excess" needed to make the sequence non-increasing. But is there a known characterization? This process is similar to the algorithm for constructing the "non-increasing" sequence from a given sequence by repeatedly adding 1 to the first element that is less than the next, which is essentially like "balancing" to make the sequence non-decreasing? Not exactly.
We might think of it as a "greedy" way to compute the final non-increasing sequence that is the least upper bound? Actually consider we want to make the sequence non-increasing by increasing some elements (only increasing allowed). The minimal sum of increases to achieve a non-increasing sequence is given by something like: we can from right to left ensure each element is at least the next? Wait, if we can only increase, to make the sequence non-increasing, we need each element to be >= next. The minimal changes to achieve that is to process from left to right? Not exactly. Actually if we are allowed to increase any element arbitrarily, to get a non-increasing sequence, we could set each element to be max of itself and the next? But that would require sometimes decreasing? No, we cannot decrease, only increase. So the minimal non-increasing sequence that is >= original pointwise is obtained by something like: from right to left, set h_i' = max(h_i, h_{i+1}')? But h_i' must be >= h_i and also >= h_{i+1}'. Since we can only increase, we can set h_i' = max(h_i, h_{i+1}'). This yields a non-increasing sequence. For example, [1,2,3] => process from right: h3' = h3 =3; h2' = max(2,3)=3; h1' = max(1,3)=3 -> [3,3,3]. This is the minimal non-increasing sequence that is pointwise >= original. In the boulder process, we ended at [3,3,3] as well. For [1,100,1] => h3'=1; h2'=max(100,1)=100; h1'=max(1,100)=100 => [100,100,1] which is what we got after 99 increments. So the boulder process seems to converge exactly to that minimal non-increasing sequence (the "non-increasing closure" via right-to-left max). Is that always true? Let's test with [2,1,3]: initial [2,1,3]. Minimal non-increasing sequence: from right: h3'=3; h2' = max(1,3)=3; h1' = max(2,3)=3 => [3,3,3]. Simulate boulders:
Start: first ascent? i=1: h1=2, h2=1 => 2>=1, so roll to i=2. At i=2: h2=1, h3=3 => 1<3, so stop at i=2 => increment h2 => [2,2,3].
Next: i=1: h1=2, h2=2 => roll to i=2; i=2: h2=2, h3=3 => 2<3, stop at i=2 => [2,3,3].
Next: i=1: h1=2, h2=3 => stop at i=1 => [3,3,3]. So indeed reached [3,3,3]. So it matches.
What about [4,1,2,3] from the example? Let's compute minimal non-increasing sequence: From right: h4'=3; h3'=max(2,3)=3; h2'=max(1,3)=3; h1'=max(4,3)=4 => [4,3,3,3]. That's exactly the final state in the example after 3 boulders. So the process converges to that final state after some number of boulders. And after that state, further boulders fall.
Thus, we have a clear characterization: The boulder process will, after a finite number of steps (equal to the total increase needed to transform the array into its "right-to-left max" non-increasing version), reach that final state. Once in that final state, all subsequent boulders fall. Moreover, during the process, each boulder increments exactly one position, and the order of increments follows a specific pattern: starting from the first ascent, eventually we fill all deficits.
So the total number of boulders that actually land (i.e., stop) before the process stabilizes is exactly the sum over i of (max needed - h_i) where the needed values are computed as the cumulative maximum from right? More precisely, the final heights f_i = max_{j>=i} h_j? Wait, we computed from right to left: f_n = h_n; for i from n-1 down to 1, f_i = max(h_i, f_{i+1}). That ensures f_i >= f_{i+1}. So the total increments needed = sum_{i=1}^{n} (f_i - h_i). That's the total number of boulders that will stop (since each boulder increases some h_i by 1, and after that many increments, the array becomes f). But is that exactly the number of boulders that stop? In the examples, yes: [4,1,2,3] -> f = [4,3,3,3], increments sum = (0)+(2)+(1)+(0)=3, and indeed 3 boulders landed. [1,100,1] -> f = [100,100,1], increments sum = 99+0+0 = 99. [1,2,3] -> f=[3,3,3], sum = 2+1+0=3. [2,1,3] -> f=[3,3,3], sum = 1+2+0? Actually h=[2,1,3]: f=[3,3,3], increments = (1)+(2)+(0)=3, matches simulation steps. So seems consistent.
Now, is it always true that the number of boulders that will ever stop is exactly that sum? Intuitively, yes, because the process is deterministic and only increases elements, and the final state is exactly the minimal non-increasing sequence that is >= original. And the process never overshoots (can't increase beyond that because if it were greater than f at some index, that would violate minimality? But could it overshoot? Suppose we have [1,2,2,3]? f = [3,3,3,3]? Let's compute: h4=3; h3=max(2,3)=3; h2=max(2,3)=3; h1=max(1,3)=3 -> f=[3,3,3,3]; increments sum = 2+1+1+0=4. Simulation we did gave 4 boulders. Good.
So if k is less than or equal to total_landings = S = sum(f_i - h_i), then the k-th boulder lands at some position. If k > S, then the k-th boulder falls (since after S boulders, the array becomes f, and all subsequent boulders fall). So answer -1 when k > S.
Now we need to output the position where the k-th boulder stops, assuming k <= S.
We could simulate step by step, but S could be as large as? With n=100 and heights up to 100, f_i could be up to max of all heights, which is at most 100 initially, but wait, the process of right-to-left max uses original heights; original max <= 100. So f_i <= 100? Not necessarily: If we have [1,2,3,...,100] for n=100, then f_i = max of tail, so f_1 = 100, f_2 = 100, ..., f_100 = 100. Then increments sum = (100-1)+(100-2)+...+(100-100?) Actually last is 100-100=0. So sum = sum_{i=1}^{99} (100 - i) = 10099 - (1+...+99) = 9900 - 4950 = 4950. So maximum S might be about 4950 for n=100 with max height 100. Actually if we have [1,100,1,100,...] pattern? Let's think worst-case: maximum S occurs when we have increasing sequence with large differences. But since each h_i <= 100, and f_i is max of tail which is at most 100, so the maximum increment per position is at most (100 - h_i). The worst sum is when we have the smallest possible h_i for many positions and f_i = 100 for them. So we could set h_1 = 1, h_2 = 1, ...? Wait, if we have many low values but f_i might be 100 for all i if the maximum overall is 100. So the sum could be up to n99? Actually if all h_i = 1 and maximum is 100 somewhere, then f_i for indices before that max become 100. But there must be at least one element with height 100 to make f_i=100 for preceding ones. For maximum sum, we want as many low values as possible before a high max. For n=100, we could set h_n = 100, and all others = 1. Then f_1..f_{99} = 100, f_100 = 100. So increments sum = (99)(100-1) = 9999 = 9801. Actually 100-1=99 for each of 99 positions, so 9999 = 9801. So S can be up to about 9801. That's within simulation range. Since n <= 100, and heights are integers up to 100 initially, the maximum total increments needed is bounded by (n-1)(max_h - min_h) but min_h at least 1, max_h at most 100, so at most 9999 = 9801. But wait, what if there are multiple high values? The sum of increments is sum_i (f_i - h_i). The maximum f_i is at most 100. The minimum h_i is at least 1. So each term at most 99. With n=100, sum <= 9900? Actually 10099 = 9900, but we must have at least one h_i = f_i, maybe the maximum element. But the maximum element could be 100, and its term 0. So sum <= 9999 = 9801 if only one 100? Could there be a scenario where all h_i = 1 and max is also 1? That gives sum 0. But to maximize sum, we want as many terms as possible with large difference, while keeping the f_i high. The f_i for i after the max are based on the max of tail; if we have multiple 100's, the earliest one gives f_i = 100 for preceding indices, but for indices after that, f_i may be less if later values are lower? But we can set the entire array to have max 100 only at the last element, and all others 1. Then f_i = 100 for all i (since tail includes the 100), so each term (100-1)=99 for i=1..99, term 100: 100-100=0 => sum=9999=9801. If we had two 100's, say at positions 50 and 100, then for i > 50, f_i = max of tail; after position 50, the max is 100, but for i=51..99, f_i = 100 because tail includes 100 at 100; but also for i=50, f_50 = max(h_50=100, f_51=100)=100, term 0; for i<50, f_i = 100; sum = (49)99 + 49499801 ~ 980100, which is okay.
But is it always true that S is bounded by about 10^4? Let's double-check constraints: h_i <= 100, n <= 100. Yes. So f_i <= 100 as it's the max of h's. So indeed each f_i - h_i <= 99, sum <= 99100 = 9900, but with at least one h_i = max, it's at most 9999 = 9801. So S is at most 9801. Good.
Thus, a simple simulation for each test case, for up to min(k, S) boulders, is feasible. We'll simulate the process: for b from 1 to min(k, S): find the first i (1 <= i < n) such that h_i < h_{i+1}. If found, increment h_i; record that the b-th boulder stops at i. If not found (i.e., the array is non-increasing), then all remaining boulders fall. But we know S is the total number we can increment before it becomes non-increasing. So we can simulate until we either have done k steps or the array becomes non-increasing. Since S is small, we can just simulate up to k steps directly (but k could be large, so we break early if k > S). But we need to know S to decide if we need to simulate full k? Actually we can simulate while b <= k, but if at some point before k we get to non-increasing state, then we know answer is -1 because the k-th boulder would fall (since the k-th is after that state). But we can precompute S to check if k > S, then answer -1 without simulation. If k <= S, we simulate exactly k steps and output the position of the k-th boulder.
However, careful: The process may not always reach the final state after exactly S steps? It should, because each step increments exactly one mountain and after S increments we get f. But need to ensure that we never increment a mountain beyond f? Could the process ever increment a mountain more than needed? That would imply that at some point h_i becomes > f_i, which would mean the sequence is not minimal non-increasing but something else. Let's test: Could we overshoot? Suppose we have [2, 1]. f = [2,1]? Actually compute: h2=1 => f2=1; f1 = max(2,1)=2. So f=[2,1]. S=0. Initially it's non-increasing? 2>=1, yes. So no boulders stop. If we had [1,2], f=[2,2], S=1. Process: first boulder stops at i=1, increment to [2,2]; now it's non-increasing (2>=2). So we didn't overshoot. What about [1,1,2]? f: from right: h3=2 -> f3=2; f2 = max(1,2)=2; f1 = max(1,2)=2 -> f=[2,2,2]; S = (1)+(1)+(0)=2. Process:
B1: stop at first i where h_i < h_{i+1}: check i=1: 1<1? false (equal). Since h1>=h2 (1>=1) so roll to i=2; at i=2: 1<2, stop at i=2 => increment h2: [1,2,2]
B2: start: i=1: 1<2 => stop at i=1 => increment h1: [2,2,2] -> done. So correct.
Now consider [1,3,2]. f: h3=2 => f3=2; f2 = max(3,2)=3; f1 = max(1,3)=3 => f=[3,3,2]; S = (2)+(0)+(0)=2. Process:
B1: i=1: 1<3 => stop at i=1 => [2,3,2]
B2: i=1: 2<3 => stop at i=1 => [3,3,2] -> now h1=3>=h2=3, h2=3>=h3=2, non-increasing. So S=2 steps, matches.
What about [2,0?] but heights >=1, so not needed.
I think it's safe: The process always increments the first index i where h_i < h_{i+1}. This is exactly the algorithm that transforms the array into its "right-to-left max" sequence. It can be proven by induction that it never increments beyond that final value because f_i is the maximum among h_i,...,h_n, and after increments, we never increase any element beyond the maximum of the original array? Actually f_i is at most the maximum of original heights, but after increments, could an element exceed the original maximum? For example, if original array is [1,2] max=2, f=[2,2]; after increments, we have at most 2. But what about [1,2,1]? Original max=2, f=[2,2,1]. After process, final heights are [2,2,1] which includes 2, not exceeding original max. But consider [1,2,2]? f=[2,2,2] includes 2. But could we ever have a scenario where an element becomes larger than the original maximum? Suppose original max is 100, but we might increment a position that already equals 100? If it's equal, then condition for stopping is h_i < h_{i+1}. If h_i = 100 and h_{i+1} = 99? That's not <, it's >, so it wouldn't stop there; it would roll if h_i >= h_{i+1}. So if a mountain is already at the max, it wouldn't be incremented because to be incremented we need h_i < h_{i+1}. But if the max occurs somewhere, the elements to its left will be increased to at most that max. The max itself is not incremented because for its index i (where h_i = max), there may be next element smaller, so condition h_i < h_{i+1} is false (since < would require h_i < h_{i+1}, but h_{i+1} <= h_i). So the maximum element never gets incremented. So heights never exceed the original maximum. Good. So f_i <= original max. Therefore, S is bounded by n*(max_original - min_possible) but min_possible at least 1, so indeed S <= 99*99=9801. So simulation up to 10^4 steps per test is trivial.
But wait: what about the constraint k up to 1e9? We need to check if k > S; if yes output -1; else simulate k steps. However, we must be careful: is it always true that the number of boulders that stop equals S? Let's prove: The process terminates exactly when the array becomes non-increasing. At termination, heights are some array g. Claim: g is exactly the "right-to-left max" array f. Proof: Suppose after some increments we have a non-increasing array g. Since we only increased heights, we have g_i >= h_i for all i. Also, g must satisfy g_i >= g_{i+1} for all i. The minimal such array (pointwise) is exactly f, because for any i, we need g_i >= g_{i+1} and g_i >= h_i. By induction from right, the minimal possible is f_i = max(h_i, f_{i+1}). Since we never decreased, we might have g_i > f_i. Could that happen? If at some point we increased an element beyond f_i, then the array would still be non-increasing, but would that be reachable? Let's examine if it's possible to overshoot f_i. To have g_i > f_i, we would need to increment a position that already meets or exceeds the required condition. But the process only increments at a position i when h_i (current) < h_{i+1}. If we have already reached a state where the array is non-increasing (so h_i >= h_{i+1} for all i), then no further increments occur. So the process stops exactly when the array becomes non-increasing. Could it become non-increasing with some g_i > f_i? For that to happen, at some step we would have increased an element beyond f_i while still having some ascent elsewhere, and later the ascent got resolved, leaving g_i > f_i. But would that cause the process to stop earlier? Possibly, but then the array would be non-increasing but with some elements larger than necessary. But is that possible given the rule? Let's try to construct a counterexample. Suppose we have [1,2,3]. f=[3,3,3]. Could we ever end with [4,3,3]? That would be non-increasing (4>=3>=3). But can we get 4 at position 1? To increment position 1, we need h1 < h2. Initially h1=1, h2=2, so we increment. After some increments, we get to h1=3, h2=3, h3=3. At that point, h1=3, h2=3: condition h1 < h2? false (3<3 false). So we wouldn't increment position 1 further. To get to 4, we would need at some point to have h1=3 and h2=4? But h2 can't be 4 because max original is 3, and we can't increase h2 beyond 3? Actually could h2 become 4? To increment h2, we need h2 < h3. Initially h2=2, h3=3 => increment h2 to 3. Then after that, h2=3, h3=3 => h2 < h3 false, so no more increments to h2. So h2 never exceeds 3. Thus h1 cannot become 4 because to increment h1 we need h1 < h2, but h2 max is 3, so h1 can be at most 3. So indeed g_i cannot exceed the original maximum. More generally, the process never increases an element beyond the maximum of the original array, because to increment i, we need i to be less than next element. The next element itself is at most the original maximum (since it's either original or increased but same argument). So by induction, all heights stay <= original maximum. Therefore, the final non-increasing array g satisfies g_i <= original max. Combined with g_i >= h_i and g_i >= g_{i+1}, the minimal such is f. But could there be a different non-increasing array with all entries <= max but not minimal? For example, suppose original = [1,2,1]; max=2. f = [2,2,1]. Could we get g = [2,2,1] only? What about [2,2,2]? That would have h3=2 > original h3=1, but can h3 become 2? To increment h3, it's the last mountain; boulders never stop at last mountain because they fall if they reach it. So h3 can never be incremented (since the only way to increase a mountain is by stopping there, which requires i < n and h_i < h_{i+1}. For i=n, there's no next, so no increment. So last mountain never changes. So h3 stays at 1. So final array must have last element = original h_n. That's an important observation: The last mountain's height never changes, because a boulder never stops at the last mountain (since if it reaches the last mountain, it falls). Also, the boulder stops only when h_i < h_{i+1} for i < n, so i = n is never a stopping point. So h_n remains constant throughout the process. In f, f_n = h_n. So indeed the final array must have last element fixed. With that fixed, the only possible non-increasing array with that fixed last element and heights <= max is uniquely determined? Let's see: Starting from right, we must have g_{n-1} >= g_n, and we want the minimal such with g_i >= h_i. Since we cannot decrease, the minimal solution is indeed f. And the process only increases until the minimal non-increasing condition is satisfied. Could it overshoot? Suppose we increase g_{n-1} beyond what is needed to be >= g_n. That could happen if at some point we increment g_{n-1} more than necessary. But would that happen? For g_{n-1} to be incremented, we need h_{n-1} < h_n. After each increment, h_{n-1} increases. Once it becomes >= h_n, the condition for stopping at n-1 becomes false. So we stop incrementing exactly when h_{n-1} becomes >= h_n. The process might overshoot if we increment when h_{n-1} is already >= h_n? But the rule only increments when h_i < h_{i+1}, so it would not increment if already >=. So we increment exactly until h_{n-1} >= h_n, i.e., the minimal amount to satisfy that condition? But could it overshoot by 1? Suppose h_{n-1}=2, h_n=3, so h_{n-1} < h_n. One increment makes h_{n-1}=3, now h_{n-1} >= h_n (3>=3). Next boulder, if it gets to n-1, condition h_{n-1} < h_n? 3<3 false, so it won't stop there. So we stop at exactly the point where equality is achieved, not overshooting. If h_{n-1} started at 1 and h_n=3, first increment: 2, still <3; second: 3, now >=, stop. So we increment until first time h_{n-1} >= h_n. That yields h_{n-1}=max(original h_{n-1}, h_n). That's exactly f_{n-1} = max(h_{n-1}, h_n). So we get exactly the minimal needed. So indeed the process yields exactly f.
Thus, S = sum_i (f_i - h_i) is the total number of boulders that ever land.
Now, the problem asks for the position of the k-th boulder. Since we can simulate up to k steps if k <= S, we can just do that. Complexity O(n * S) which is at most 100*10000 = 1e6 per test case, but sum n is 100, so total about 1e6, fine.
But we must be careful: the simulation might need to find the first i where h_i < h_{i+1} each time. That's O(n) per boulder. With up to 10^4 boulders, that's 1e6 operations, fine. Also t up to 100, but total sum of n <= 100, so worst-case many test cases with small n, each having S up to about 9801? Actually each test case's S might be up to 9801, but if we have 100 test cases each with n=1? Then S=0. But worst-case sum of n <=100, so the total number of test cases could be up to 100 but each n could be small, but the sum of S across tests could be at most? Each test with n=100? But sum of n <=100, so there can be at most one test with n=100, or several with smaller n. So total S across tests is bounded by sum over tests of (n_i * 99) maybe, but each test's S <= (n_i-1)99? Actually more precise: For each test, S <= (n_i)(max_h - min_h) but max_h <= 100, min_h >=1, so <= 99n_i. Since sum n_i <= 100, total S across all tests <= 99 * 100 = 9900. That's even smaller! Wait, we need to check: S = sum_i (f_i - h_i). For each i, f_i is at most max of original array, which is at most 100. But if max is less than 100, f_i could be less. However, we can bound S by n_i * (max_h - min_h) but min_h >=1, max_h <=100, so at most 99 per element, yes. So sum of S across tests <= 99 * sum n_i <= 99100 = 9900. So total simulation steps across all test cases is at most 9900. That's extremely small. So we can just simulate straightforwardly without even precomputing S; we can simulate until either we've processed k boulders or we find that the next boulder would fall (i.e., array becomes non-increasing). But k could be as large as 1e9, so we cannot simulate k steps if k is huge and S is small. But we can break early: after each boulder, we check if array becomes non-increasing; if after some number of boulders (say m) the array becomes non-increasing, then for any k > m, the k-th boulder falls. So we can simulate until either we have thrown k boulders, or we reach a non-increasing state. Since total possible increments before non-increasing is S <= 9900, we will stop after at most S steps. So we can simulate up to min(k, S+1) steps? Actually we need to know the outcome for k-th boulder. If k > number of boulders that actually land (i.e., increments), then answer is -1. So we can simulate until we either reach k steps or we find that the next step would be a fall. But careful: The state may become non-increasing after some increments; then the next boulder falls. So if we have simulated m boulders that landed, and then the array is non-increasing, then for the (m+1)-th boulder, it will fall. So we can compute m = number of boulders that land before the array becomes non-increasing. That's exactly S. So we can compute S easily by f computation. Then if k > S -> -1 else simulate k steps to get position.
But we could also simulate without precomputing S: just simulate until we either have done k steps or we detect that no stopping position exists. But we need to be careful: If k is huge, we cannot simulate k steps, but we can stop early when we detect that the array becomes non-increasing. Since after that point, all boulders fall, we can compute that the number of landing boulders is the number we've already simulated, say m. Then if k <= m, we would have already output during simulation. But if k > m, we know answer is -1. So we can simulate up to m steps (which is at most S <= 9900) and then check if k <= m. So we can just simulate until the array is non-increasing. That will take at most S steps, regardless of k. Then we have a list of positions for each boulder that landed (in order). Then if k is <= length of that list, output position; else output -1.
That is straightforward and safe.
Let's confirm with examples: first case n=4, heights [4,1,2,3]. Simulate until non-increasing. As we saw, after 3 steps array becomes [4,3,3,3] which is non-increasing. So list positions = [2,3,2]. For k=3, output 2; for k=5, output -1.
Second case: [1,8]. Simulate: start: check i=1? Since n=2, if h1 < h2? 1<8, so stop at i=1, increment to [2,8]. Next: 2<8, stop at 1 => [3,8]. Continue until h1 becomes 8. So increments needed: 7 steps. After 7 steps, array [8,8] non-increasing. So positions list = [1,1,1,1,1,1,1] (7 times). For k=7, output 1. If k=8, would be -1. Works.
Third case: same as first but k=5 > 3, output -1.
Fourth case: [5,3,1]. Check: initial array: 5>=3, 3>=1, so non-increasing. So no boulder lands. List empty. For any k>=1, answer -1.
Thus algorithm per test case:
Read n, k, array h[1..n].
Initialize an empty list positions (or we can just simulate until either we have k boulders landed or we finish).
Actually we need the position of the k-th boulder if it lands. So we can loop boulder_num from 1 to potentially large, but we will break early.
We'll maintain current heights.
We'll loop while true:
Find the smallest i (1-indexed) such that i < n and h[i] < h[i+1].
If no such i exists:
// array is non-increasing, so all further boulders fall.
break out of loop.
Else:
// This boulder lands at i.
Increment h[i] by 1.
Record that this boulder lands at i.
If we have recorded k landings, we can output i and break (and skip further processing).
After loop, if we didn't output yet (i.e., number of landings < k), output -1.
Since number of landings is at most S <= 9900, the loop will iterate at most that many times. So it's fine.
Edge Cases: n=1. Then there is no i < n, so no boulder can ever stop; they all fall (since they start at the only mountain? Actually if n=1, the boulder starts at mountain 1, which is also the last mountain, so it falls immediately). So answer always -1. Our algorithm: no i such that i < n (since n=1, i from 1 to 0 none), so break immediately, positions list empty, output -1. Good.
Thus implementation is simple.
But we need to verify that the process indeed stops after at most S steps and doesn't continue indefinitely. Given our reasoning, it does. So simulation is safe.
Now, is there any scenario where the process may require more than S steps because a boulder falls before the array becomes non-increasing? No, boulder falls only when array is non-increasing (or when it reaches last mountain). So if array is not non-increasing, there will always be some i where h_i < h_{i+1} (since if not non-increasing, that means there exists i with h_i < h_{i+1}; the first such i will be found). So each boulder lands until non-increasing. So number of landings equals number of steps until first time array becomes non-increasing.
Now we should confirm the bound: sum of n <=100 ensures total steps across test cases is small. But what if n=100 and heights like [1,1,...,1,100]? Then S=99*99=9801 steps. That's fine.
Implementation details: Use 0-index or 1-index. We'll use 0-index for array but positions are 1-indexed in output.
Pseudo:
for each test case:
read n, k
read list h of size n
positions = []
for b in range(k): # but we will break early if no more landings
found = False
for i in range(n-1):
if h[i] < h[i+1]:
h[i] += 1
positions.append(i+1)
found = True
break
if not found:
break
if len(positions) == k:
print(positions[-1])
break
else: # if loop finished without break? Actually careful.
# Actually if we break due to not found, we need to output -1 if we didn't print.
# Simplify: after loop, if len(positions) >= k, we printed; else print -1.
But using a for loop with range(k) may be dangerous because k could be large (1e9) and we break early when no more landings, but if there are many landings (up to 10^4) it's still fine to loop up to 1e9? Actually we cannot loop to k because it's huge. We need to loop until we either have k landings or no more landings. So we should use a while loop:
landings = 0
while landings < k:
find first i where h[i] < h[i+1]
if not found:
break
else:
h[i] += 1
landings += 1
if landings == k:
print(i+1)
break
if landings < k:
print(-1)
But note: we need to break after printing to avoid printing -1 as well. So we can use a flag.
Alternatively, we can compute S first, but it's unnecessary.
Given that the maximum number of landings is small, we can also precompute all landing positions until non-increasing, store them in a list, then if k <= len(list) output list[k-1] else -1.
So:
landings = []
while True:
for i in range(n-1):
if h[i] < h[i+1]:
h[i] += 1
landings.append(i+1)
break
else:
break
if k <= len(landings):
print(landings[k-1])
else:
print(-1)
Thus the solution is trivial given the constraints.
But wait, is there any hidden catch? The initial statement: "you will start throwing boulders from the top of the first mountain one by one". It says "the boulder will roll to the next mountain" if h_i >= h_{i+1}. So the boulder is initially at the first mountain? Or does it start from the top? Likely it starts at mountain 1. So our simulation matches.
One nuance: If n=1, there is no next mountain, so the boulder falls immediately. Our simulation (no i < n) leads to immediate break and landings empty, correct.
Edge: When a boulder stops at i and increases h_i, could that cause a situation where now h_{i-1} < h_i (if previously equal) and become the first ascent for the next boulder? Our simulation by scanning from i=0 each time will correctly find the first i where h_i < h_{i+1}. That includes the possibility that the new ascent appears at an earlier index than before.
Now, since sum n <=100, we can even afford to just scan each time. Time complexity O(total_landings * n) <= about 10^4 * 100 = 1e6, trivial.
Thus the solution is straightforward simulation with early termination. No need for fancy math.
But wait, are we sure that total_landings (S) is always <= (n-1)99? Let's double-check the bound: The last element never changes, and it's at most 100. So all f_i are at most 100. However, is it possible that the original maximum is, say, 100, but we could end up increasing some element beyond 100? As argued, no. Because to increase a mountain i, we need h_i < h_{i+1}. If h_{i+1} is at most original maximum (which is <=100), then h_i can never exceed that maximum. However, what if the maximum is not unique? Suppose original max is 100 at position j. For i < j, we might increment i until it becomes 100, but not beyond because when h_i reaches 100, h_{i+1} might be 100 (if j = i+1 or if other). But could h_{i+1} become >100? No, because h_{i+1} itself is at most original max (or gets increased but never beyond original max). But wait, if original max is 100, but we might increase an element that is not the maximum beyond 100 if it has a neighbor that is also increased beyond 100? But as argued, to increase any element, it must be less than its next. The next cannot exceed original max because it can only be increased when it is less than its next, and the chain ends at the original max (which never gets increased). Could we have a chain where the maximum is not the only element that never gets increased? Actually consider array [1,2]. Original max=2 at position2. After first boulder, h1 becomes 2. Now both are 2. max still 2. No element exceeds 2. Now suppose we had [1,2,2]. Original max=2 at positions 2 and 3. Last element (position 3) never changes. So max stays 2. So yes, no element can exceed original max. So f_i <= max_original <= 100. So S <= n99 (but because last element fixed, we could bound more tightly, but overall S <= 9900 given sum n <=100). So we are safe.
Thus the solution is trivial.
But wait, there's a nuance: In the problem statement, they say "You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is h_i): ..." So the boulder is initially at mountain 1. So our simulation is correct.
Now, confirm with sample input:
4
4 3
4 1 2 3
2 7
1 8
4 5
4 1 2 3
3 1
5 3 1
Our simulation yields output 2,1,-1,-1. Good.
Now, consider a tricky case: n=3, h=[2,2,1]. Initially: 2>=2, 2>=1 so non-increasing? Actually 2>=2, 2>=1 -> non-increasing, so all boulders fall. Our simulation: no i with h_i < h_{i+1} (since 2<2? false, 2<1? false). So landings empty, answer -1 for any k. That's correct.
Now, what about n=3, h=[2,1,2]? This is not non-increasing because 1<2 at i=2? Actually indices: i=1: 2>=1, i=2: 1<2 => ascent at i=2. So first boulder will roll to i=2? Let's simulate: start at i=1: h1=2, h2=1 -> 2>=1, so roll to i=2. At i=2: h2=1, h3=2 -> 1<2, stop at i=2, increment h2 => [2,2,2]. Now array is non-increasing (2>=2>=2). So 1 landing. So answer for k=1 is 2, for k>=2 is -1. Our simulation: first iteration: i=1? using 0-index: i=0: 2<1? false; i=1:1<2 true -> increment h[1]=2, landings=[2]. Next iteration: no i with ascent -> break. So output correct.
Now, consider n=5, h = [1, 2, 3, 4, 5]. S = sum(f_i - h_i) with f=[5,5,5,5,5] => increments = 4+3+2+1+0 =10. So at most 10 landings. Simulate: positions? Our simulation will produce something. That's fine.
Now, we should also consider that the process might produce the same position multiple times, but that's okay.
Thus the algorithm is simple.
Given that the problem likely expects an efficient solution beyond simulation? But constraints sum n <= 100, so simulation is fine. Actually maybe the intended solution is to simulate until the array becomes non-increasing because the maximum number of changes is limited. So it's fine.
Now, we must be careful about integer overflow? Not.
Implementation details: We'll parse input, for each test case, we do:
n, k = map(int, input().split())
h = list(map(int, input().split()))
landings = []
while True:
pos = -1
for i in range(n-1):
if h[i] < h[i+1]:
pos = i
break
if pos == -1:
break
h[pos] += 1
landings.append(pos+1)
if k <= len(landings):
print(landings[k-1])
else:
print(-1)
Time complexity: O(total_landings * n) <= 10010099? Actually total_landings <= 9900, n <= 100, so <= 990k ops.
But note: For each landing, we scan from i=0 each time. However, we could potentially optimize by tracking the first ascent, but not needed.
Double-check bound: For n=100, S could be up to 9801. So scanning from i=0 each time yields about 9801 * 100 = 980100 operations per test case, worst-case. Sum over tests <= 100*100? Actually sum n <= 100, so worst-case one test with n=100 and S=9801 => ~1e6 ops. That's fine.
Thus we can implement.
Now, there's a subtlety: The problem says "k — the number of boulders you have." It might be that we throw exactly k boulders, and we need to determine the position of the k-th boulder (if it stops) or -1 if it falls. Our simulation simulates exactly until we either have thrown k boulders (or earlier break). But we simulate only until no more landings, but we must account for the possibility that after some landings, the array becomes non-increasing, and then we continue throwing the remaining boulders, but they all fall. So we only need to know if the k-th boulder is among those that landed. So we don't need to simulate falling boulders.
Thus the solution stands.
Let's test some random cases manually to be sure.
Case: n=2, h=[5,1], k=1. Initially: h1=5, h2=1 => h1 >= h2, so boulder rolls to i=2 and falls. So answer -1. Our simulation: check i=0: 5<1? false, no i, break. landings empty => -1. Good.
Now, what about n=3, h=[1, 100, 100]? f: from right: h3=100; f3=100; f2 = max(100,100)=100; f1 = max(1,100)=100 => S = 99. Simulate: first boulder: i=0: 1<100 -> increment h0=2, landings=[1]; h=[2,100,100]. Next: i=0:2<100 -> increment to 3... Continue until h0=100. So 99 landings at position 1. After that array [100,100,100] non-increasing. So for k=99 -> 1; k=100 -> -1. Our simulation will produce 99 landings then break. Good.
Now, consider a scenario where the first ascent is not at the earliest position after some increments. For instance, h=[2, 1, 2] we already did.
Thus implementation is correct.
Now, let's think about possible pitfalls: The input constraints say h_i <= 100, but after increments we might go up to 100, which is still within 100? Actually we can exceed 100? No, because original max <= 100, and heights never exceed original max, so they stay <= 100. So no overflow. But what if original max is less than 100, like 50, then heights remain <=50. So still within small range.
Now, I think this is the intended solution due to constraints sum n <= 100.
However, I've seen a similar problem on Codeforces: "Boulders" maybe? Actually I recall a problem "B. Boulders" from Codeforces Round #... But I'm not sure. But anyway.
One more check: Could the process ever cause a boulder to stop at position i that is not the first ascent? According to rule, it rolls from start, and stops at the first i where h_i < h_{i+1}. So yes, we always stop at the first ascent. So scanning from left is correct.
Thus final answer: implement as above.
Now, confirm with a more complex example to ensure that scanning from left each time gives correct order. Suppose h = [3, 3, 2, 4]. Let's simulate manually. Initially: i=0: 3<3? false; i=1: 3<2? false; i=2: 2<4? true => stop at i=2 (3rd mountain), increment to [3,3,3,4]. Next: i=0:3<3? false; i=1:3<3? false; i=2:3<4? true => stop at i=2 again, increment to [3,3,4,4]. Next: i=0:3<3? false; i=1:3<4? true => stop at i=1, increment to [3,4,4,4]. Next: i=0:3<4? true => stop at i=0, increment to [4,4,4,4]. Then non-increasing. So landings: [3,3,2,1] (positions 3,3,2,1). Our scanning method would produce same.
Now compute f: from right: f4=4; f3 = max(2,4)=4; f2 = max(3,4)=4; f1 = max(3,4)=4 => f=[4,4,4,4]; increments sum = (1)+(1)+(2)+(1?) Actually h1=3->4 diff1; h2=3->4 diff1; h3=2->4 diff2; h4=4->4 diff0 => total 4. matches landings count. So good.
Now, check if there is any scenario where scanning from left could incorrectly identify a stop before the correct one? Since boulder physically rolls and checks condition at each mountain; if it rolls to a mountain, it must have satisfied h_i >= h_{i+1} for all previous. So indeed it's the first i with h_i < h_{i+1}. So scanning from left to find first such i is exactly what the boulder does.
Thus code is fine.
Now, I should also consider the possibility that the array becomes non-increasing before we have processed k landings, but we might have simulated more landings than necessary? We stop when no ascent found. So we break. Then we check length of landings. If k <= length, output; else -1.
One more thing: Since k can be as large as 1e9, and landings list length might be up to 10k, we shouldn't store all if we only need the k-th. But we can stop early when we reach the k-th landing. So we can break as soon as we have recorded k landings. That saves time if k is small. So we can combine:
landings = 0
while True:
pos = -1
for i in range(n-1):
if h[i] < h[i+1]:
pos = i
break
if pos == -1:
break
h[pos] += 1
landings += 1
if landings == k:
print(pos+1)
break
if landings < k:
print(-1)
That's efficient and doesn't store all positions.
But careful: We need to break out of the outer loop after printing. In code, we can set a flag or use a function. We'll use a found boolean.
Pseudo:
def solve():
read t
for _ in range(t):
n,k = map(int, input().split())
h = list(map(int, input().split()))
landed = 0
ans = -1
while landed < k:
pos = -1
for i in range(n-1):
if h[i] < h[i+1]:
pos = i
break
if pos == -1:
break
h[pos] += 1
landed += 1
if landed == k:
ans = pos+1
break
if ans != -1:
print(ans)
else:
print(-1)
This will break when we've processed k landings, or when no more landings.
Edge: if k==0? But k >= 1, so fine.
Now, what if k is huge but we break early when array becomes non-increasing, so we don't loop many times.
Now, double-check: In the while condition we have landed < k, so if we have already landed k boulders, we break out after printing ans, or if we exit because no more landings, then landed < k and we go to print -1.
All good.
Now, test with the sample input:
Case1: n=4,k=3, h=[4,1,2,3].
landed=0, pos: find first i: i=0:4<1 false; i=1:1<2 true => pos=1; h[1]=2; landed=1; not k.
landed=1, pos: i=0:4<2 false; i=1:2<2 false; i=2:2<3 true => pos=2; h[2]=3; landed=2; not k.
landed=0, pos: i=0:1<8 true => pos=0; h[0]=2; landed=1; not k.
repeat, each time pos=0, increment. After 7 increments, at landed=7, h[0]=8, h[1]=8; before checking for the 7th boulder? Wait: After the 6th boulder (landed=6), h[0]=7, h[1]=8. Then the 7th boulder: find pos: i=0:7<8 true => pos=0; h[0]=8; landed=7; now landed==k, ans=1; break. Print 1.
But after this, the array becomes [8,8]; but we don't check for further boulders.
Now, if k were 8, our loop would do:
after 7th boulder, landed=7, not yet k.
Next iteration: find pos: i=0:8<8 false => pos=-1 => break from while. Then landed (7) < k (8), ans still -1, print -1. So correct.
Case3: similar to case1 but k=5. We'll simulate as before; after 3 landings, array becomes [4,3,3,3]; landed=3 <5, loop continues: find pos: no i found => break; print -1.
Case4: n=3,k=1, h=[5,3,1]; first iteration: no pos => break; landed=0<1 => print -1.
All correct.
Now, we must consider if the boulder stops at a mountain when h_i < h_{i+1}. That's exactly our condition. Also note that when i = n-1, we check h[n-1] < h[n]? That's allowed. So our loop i from 0 to n-2 inclusive? Actually range(n-1) goes from 0 to n-2, because n-1 is excluded. That's correct because we need i < n, so i up to n-2? Wait: Condition applies for i from 1 to n-1? Actually i is current mountain index. If we are at i = n, it's last mountain and boulder falls. So the rule to stop is for i < n: if h_i < h_{i+1}. So the last i we need to consider is i = n-1 (since h_{n} exists only if n-1+1 = n). But i = n-1 is the second last mountain? Let's check: If i = n-1, then h_i and h_{i+1} = h_n exist. So we can check condition at i = n-1. So the loop should go i = 0 to n-2? Actually in 0-index, indices 0..n-1 correspond to mountains 1..n. Condition for i (0-index) such that i+1 < n (i <= n-2) corresponds to checking pair (i,i+1) where i+1 is within bounds. But for i = n-1, there is no i+1 (since i+1 = n out of bounds). However, in the process, the boulder never actually "checks" at i = n because it's the last mountain and falls. But does it check at i = n-1? Yes, because when the boulder arrives at mountain n-1, it checks against mountain n. So we need to consider i = n-1 (0-index) as a possible stopping point. In our loop for i in range(n-1): in Python, range(n-1) gives 0,1,..., n-2. That's missing i = n-1. So we need to adjust: we should consider i from 0 to n-2? Actually if n=2, mountains indices 0 and 1. The boulder starts at mountain 0. It checks if h0 < h1? That's at i=0. If not, it rolls to mountain 1. At mountain 1, it's the last mountain, so it falls; we don't check condition there because there is no next. So the only possible stopping position is i=0. So our loop for n=2 with range(n-1) gives i=0 only, correct. For n=3, mountains 0,1,2. The boulder can stop at i=0 or i=1 (since i=2 is last). So loop should include i=0 and i=1. range(n-1) gives 0,1 -> correct. For n=3, n-1=2, range(2) gives 0,1. So it's fine. Because the last index we need to check is n-2? Wait, n=3: i can be 0,1. i=1 is the second last. So yes. For general n, possible stopping positions are i = 0 .. n-2 (1-indexed positions 1 .. n-1). Because i = n-1 is the last mountain and we never stop there; we only fall if we reach it. So our loop should be for i in range(n-1) (0 to n-2) which covers all positions where there is a next mountain. So it's correct. Good.
Thus implementation is correct.
Now, some may worry about integer overflow for k (up to 1e9) but we don't loop that many times.
Now, I'll write the final solution accordingly.
But wait, is there any scenario where the boulder could stop at a mountain after rolling from earlier ones but the first ascent is not the first because the boulder might skip some mountains? The boulder moves to next only if h_i >= h_{i+1}. So it will continue as long as the heights are non-increasing. So the first time it encounters an increase (h_i < h_{i+1}) it stops. So scanning from left until we find an i where h_i < h_{i+1} is exactly the boulder's path, assuming we start at i=0 and we only consider indices that the boulder actually visits? But what about the fact that the boulder moves only if h_i >= h_{i+1}. If we simply scan from left, we might find an i where h_i < h_{i+1}, but the boulder might not have reached that i because it could have stopped earlier at a previous i where h_i < h_{i+1}? Actually scanning from left, the first i with h_i < h_{i+1} is the earliest possible stop. That's correct. However, we must also ensure that the boulder actually can reach that i. If there is an earlier i where h_i < h_{i+1}, it would have stopped there. So by taking the first such i, we guarantee that the boulder would encounter that condition first. But what if for some earlier j, h_j >= h_{j+1}, so the boulder rolls over j, and we later encounter an i where h_i < h_{i+1}. That's fine. So scanning from left until we find the first ascent yields the stopping point.
Thus algorithm is correct.
Now, one more nuance: The boulder's movement is stepwise: it starts at mountain 1. At each step, it checks the condition at current mountain. If h_i >= h_{i+1}, it moves to i+1 and repeats. So if we find the first i such that h_i < h_{i+1}, we must be sure that for all j < i, h_j >= h_{j+1}. Our scanning method does not explicitly check that, but it finds the first i where h_i < h_{i+1}. That implies that for all j < i, h_j >= h_{j+1}? Not necessarily! Example: h = [5, 2, 4]. The first i where h_i < h_{i+1} is i=1? Let's check: i=0: 5<2? false (5 >=2). i=1: 2<4? true. So first ascent is i=1. But what about j=0? 5>=2, so that's fine. Now consider h = [5, 6, 2]. First i: i=0: 5<6 true -> first ascent at i=0. So scanning yields i=0. But what if we had h = [5, 2, 6]? Check: i=0: 5<2? false; i=1: 2<6 true => first ascent at i=1. For j=0, 5>=2 holds. So indeed if the first i where h_i < h_{i+1} occurs at i, then for all j < i, we must have h_j >= h_{j+1}, because if for some j < i we had h_j < h_{j+1}, that would be an earlier ascent. So the property holds. So scanning works.
Thus implementation stands.
Now, we can also consider an alternative solution: compute the final state f and then for each boulder, simulate its path quickly using precomputed "next ascent" pointers, but it's unnecessary.
I'll produce final code.
Edge Cases: t up to 100, sum n <=100, so reading input is straightforward.
Now, I'll produce the answer.
But before finalizing, let's double-check the note in the problem: In the first test case, they got positions [2,3,2]. Our simulation gave positions [2,3,2] as well. Good.
One more scenario: When the boulder rolls, it checks condition at the current mountain. If the condition is false (h_i >= h_{i+1}), it moves to the next mountain. Could it be that after moving, the boulder might skip checking the condition at the new mountain if it's the last? Actually it checks again. So all good.
Now, the answer is ready.
I'll now write the final solution in Python.
Implementation details:
Use sys.stdin.read() to parse quickly, but not necessary.
For each test case, implement the while loop.
Now, I'll think about potential pitfalls: The array is modified in place. The loop may run for many iterations but still within bound.
I will now code.
But wait, sum n <= 100, but S could be up to about 9801 per test case. So if we have, say, 10 test cases each with n=10 and S maybe around 900 each, total steps <= 9000, fine.
Now, what about if k is small like 1, we break early. So it's efficient.
Now, I'd like to test with a random generated case to ensure no off-by-one: n=1, h=[5], k=1. Our code: n=1, for i in range(n-1)=range(0) gives no iteration, pos stays -1, so break out of while, landed=0 <1, ans=-1, print -1. That's correct.
Now, n=100, h all 1 except last = 100. Then S = 99*99 = 9801. Simulating would take ~9801 * 100 = ~1e6 operations, okay. Python can handle.
Now, I think it's fine.
One more nuance: The problem states: "You want to find the position of the k-th boulder or determine that it will fall into the waste collection system." So if the k-th boulder falls, output -1. So our logic matches.
Thus answer.
Now, I will output the code.
But there's a catch: Could the boulder ever stop at a position that is not an index from 1 to n-1? It always stops at some mountain i < n, so positions are 1..n-1. So output is between 1 and n-1.
Now, I'll produce final code.
Given that the problem is from Codeforces (maybe problem 1355B? No, that's something else). But anyway.
Now, as an extra, I could also compute S and break early if k > S without simulation, but simulation already stops early when no more landings, and the maximum number of steps is small, so it's okay.
Thus final answer.
Let's prepare a final message with explanation and code. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
k = int(next(it))
h = [int(next(it)) for _ in range(n)]
code
landed = 0
answer = -1
# Simulate until we have thrown k boulders or no more can land
while landed < k:
pos = -1
# find first mountain where height is less than the next one
for i in range(n - 1):
if h[i] < h[i + 1]:
pos = i
break
if pos == -1: # array is non-increasing → all remaining boulders fall
break
h[pos] += 1
landed += 1
if landed == k:
answer = pos + 1 # convert to 1‑based index
break
out_lines.append(str(answer))
sys.stdout.write("\n".join(out_lines))
99 + (50? Actually let's compute: i=1..49: 99 each = 4851; i=50:0; i=51..99: each 99? For i=51..99, h_i=1, f_i=100 => 49 positions? Actually positions 51 to 99 are 49 positions (since 99-51+1=49). So sum = 49
99 = 2
99 = 9702, plus i=50 term 0 = 9702. That's less than 9801. So single max at end gives max sum. So maximum S <= 9801 for n=100. That's about 10^4. So we can definitely simulate step by step for all boulders that land, because k up to 1e9 but if k > S we just output -1; if k <= S, then S <= 10^4, so we can simulate k steps directly. However, need to consider that there could be multiple test cases, but sum of n <= 100, and each test case's S is bounded by similar magnitude, so total simulation steps across all test cases would be at most sum of S which could be up to 100