In late autumn evening n robots gathered in the cheerful company of friends. Each robot has a unique identifier — an integer from 1 to 109.
At some moment, robots decided to play the game "Snowball". Below there are the rules of this game. First, all robots stand in a row. Then the first robot says his identifier. After that the second robot says the identifier of the first robot and then says his own identifier. Then the third robot says the identifier of the first robot, then says the identifier of the second robot and after that says his own. This process continues from left to right until the n-th robot says his identifier.
Your task is to determine the k-th identifier to be pronounced.
Input
The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ min(2·109, n·(n + 1) / 2).
The second line contains the sequence id1, id2, ..., idn (1 ≤ idi ≤ 109) — identifiers of roborts. It is guaranteed that all identifiers are different.
Output
Print the k-th pronounced identifier (assume that the numeration starts from 1).
Examples
Input
2 2
1 2
Output
1
Input
4 5
10 4 18 3
Output
4
Note
In the first sample identifiers of robots will be pronounced in the following order: 1, 1, 2. As k = 2, the answer equals to 1.
In the second test case identifiers of robots will be pronounced in the following order: 10, 10, 4, 10, 4, 18, 10, 4, 18, 3. As k = 5, the answer equals to 4.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given: n up to 100,000, k up to min(2e9, n(n+1)/2). There are identifiers id1..idn, all distinct. The game: robots stand in a row from 1 to n. The pronunciation order: first robot says his id; second robot says id1 then his own; third robot says id1, id2, then his own; and so on. So the overall sequence is: For i from 1 to n: for j from 1 to i: pronounce id_j. So the sequence S is concatenation of prefixes: prefix 1, prefix 2, ..., prefix n.
We need to find the k-th element of S (1-indexed). So we need to locate which prefix and which position within that prefix.
Specifically, let total length = n(n+1)/2. We need to find the smallest i such that sum_{t=1}^{i} t >= k. Actually the cumulative lengths: after i prefixes, total length = i(i+1)/2. So find i where i(i+1)/2 >= k and (i-1)i/2 < k. Then the position within the i-th prefix is j = k - (i-1)i/2 (since before the i-th prefix, we had (i-1)i/2 elements). And j runs from 1 to i, and it corresponds to id_j. So the answer is id_j.
But careful: The problem says: "First, all robots stand in a row. Then the first robot says his identifier. After that the second robot says the identifier of the first robot and then says his own identifier. Then the third robot says the identifier of the first robot, then says the identifier of the second robot and after that says his own. This process continues from left to right until the n-th robot says his identifier."
Interpretation: Indeed, the sequence: robot 1 says id1. Robot 2 says id1, id2. Robot 3 says id1, id2, id3. So it's exactly as described.
Thus the answer is simple: find i such that i(i+1)/2 >= k, j = k - i(i-1)/2, answer = id_j.
But is there any trick? n up to 100k and k up to ~2e9, but n(n+1)/2 could be up to about 5e9 (since 100k*100001/2 ~5e9). But k is at most min(2e9, total). So we can compute i by solving quadratic: i(i+1)/2 >= k. i up to n (since k cannot exceed total length). Since n is 100k, we could also binary search i from 1 to n (O(log n)). Or directly compute floor of (sqrt(8k+1)-1)/2? That works. But careful with integer overflow: compute with 64-bit. Use long long.
Thus algorithm:
Read n, k.
Read array id[1..n].
Compute i = floor? Actually we need smallest i s.t. i(i+1)/2 >= k. Compute i = ceil((sqrt(1+8k)-1)/2). But can also binary search: low=1, high=n, while low<high: mid = (low+high)/2; if mid*(mid+1)/2 >= k then high=mid else low=mid+1. i=low.
Then prev = (i-1)*i/2 (total before this prefix).
pos = k - prev (1-indexed within prefix).
Output id[pos].
Check with examples:
Example1: n=2,k=2. i: i(i+1)/2>=2? i=1:1<2; i=2:3>=2 => i=2. prev=1*2/2=1? Actually (i-1)i/2 =12/2=1. pos=2-1=1? Wait k=2, prev=1, so pos=1? That gives id_1 =1. But expected answer 1. But sample output says 1? Actually sample output says 1. But check: Sequence: 1,1,2. k=2 -> second element is 1. Yes, answer 1. id_1=1, correct. pos=1? Actually if i=2, the prefix is id1,id2. The first element of that prefix is id1, second is id2. Since pos=1, that is id1. Correct.
But wait: According to our reasoning, k=2, after first prefix (length1) we have length1=1. So we still need one more: second prefix's first element. So pos=1. Good.
Example2: n=4,k=5. Total lengths: i=1:1; i=2:3; i=3:6. Since 3<5<=6 => i=3. prev=3; pos=5-3=2. id_2 =4. Output 4. Matches sample.
Thus solution is trivial. But why is k limited to min(2e9, total)? Possibly to avoid overflow? But with 64-bit it's fine.
Now confirm constraints: n <= 100,000; k up to min(2e9, n(n+1)/2). So k fits 32-bit signed? Up to 2e9. But we should use 64-bit for ii to avoid overflow. For binary search, compute mid(mid+1)/2 using long long.
Edge cases: k=1 => i=1, pos=1 => id1. k = total => i=n, pos = n? Since total = n(n+1)/2; prev = (n-1)n/2; k - prev = n(n+1)/2 - n(n-1)/2 = n => id_n.
All good.
Thus solution is straightforward.
But wait, the problem statement may be from Codeforces? I recall a problem "Snowball" from Codeforces? Possibly there is a twist: The game might be played with robots saying identifiers in a different order? Actually reading again: "First, all robots stand in a row. Then the first robot says his identifier. After that the second robot says the identifier of the first robot and then says his own identifier. Then the third robot says the identifier of the first robot, then says the identifier of the second robot and after that says his own. This process continues from left to right until the n-th robot says his identifier."
Yes.
Thus answer is id_{index} where index = k - t(t-1)/2 with t = ceil((sqrt(1+8k)-1)/2).
But I want to double-check: There is no off-by-one? Let's derive:
The sequence is: For i = 1 to n, we output the prefix of length i: id1, id2, ..., id_i.
Let f(i) = i(i+1)/2 be total after i prefixes. We want to find i such that f(i-1) < k <= f(i). Then position in the i-th prefix is k - f(i-1). And that index j runs from 1 to i, corresponding to id_j.
Yes.
Thus solve.
Implementation:
Use long long for calculations.
Read n,k as int? k can be up to 2e9, fits in int32? 2e9 > 2^31-1? Actually 2^31-1=2,147,483,647, so 2e9 < 2.147e9, so fits in signed 32-bit. But safer to use long long.
Use binary search for i: low=1, high=n. While low<high: mid=(low+high)/2; if (mid*(mid+1LL)/2 >= k) high=mid else low=mid+1. After loop i=low.
Then pos = k - (i-1)*i/2 (as long long, but pos <= n <=100k, so int).
Print id[pos-1] if zero-indexed array.
Time complexity O(n + log n) = O(n). Space O(n) for ids.
But is there any catch? The problem statement might be different: Maybe the robots say identifiers in a different order? Let's re-read: "First, all robots stand in a row. Then the first robot says his identifier. After that the second robot says the identifier of the first robot and then says his own identifier. Then the third robot says the identifier of the first robot, then says the identifier of the second robot and after that says his own. This process continues from left to right until the n-th robot says his identifier." That matches.
But I recall a Codeforces problem "K-th Snowball" or something? Actually maybe it's Codeforces Round #146? Might be problem "A. Snowball" or "B. Snowball"? Not sure. But I think it's correct.
Wait: There is a known problem "Snowball" from Codeforces #166? Actually I recall a problem "Snowball" where there is a row and they say identifiers but they might say them in reverse? Let's check: In the description, "the third robot says the identifier of the first robot, then says the identifier of the second robot and after that says his own." That is prefix order.
Thus solution is as described.
But I'd also consider if the input size of k up to 2e9, n up to 1e5, but total could be >2e9, but we only need to handle k up to min. So using binary search over n (1e5) is trivial. Alternatively, we can compute i via integer arithmetic: i = floor((sqrt(1+8*k)-1)/2) but need to adjust. But binary search is safe.
Edge Cases:
n=1, k=1: i=1, pos=1 => id1.
n=100000, k= total= n(n+1)/2, i=n, pos=n => id_n.
Thus answer.
I'll produce final code.
But wait: There might be a nuance: The robots "says the identifier of the first robot and then says his own identifier." Could it be that each robot says the identifiers of the robots before him in order from that robot? That is exactly what we did. So yes.
Thus solution is simple.
However, before finalizing, I'll think about if there is any alternative interpretation that could change answer. Suppose the process: first robot says id1; second robot says id1, id2; third robot says id2? No, it says "the identifier of the first robot, then says the identifier of the second robot and after that says his own." So it's prefix.
Thus all good.
Now about input: The second line contains n integers: id1...idn.
We must output the k-th identifier.
Implementation details:
cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int n;
ll k;
cin >> n >> k;
vector<int> ids(n);
for (int i=0; i<n; i++) cin >> ids[i];
// find i
int low = 1, high = n, i = n;
while (low < high) {
int mid = (low + high) / 2;
if (mid * (mid + 1LL) / 2 >= k) {
high = mid;
} else {
low = mid + 1;
}
}
i = low;
ll prev = (i-1) * 1LL * i / 2;
int pos = k - prev; // 1-indexed
cout << ids[pos-1] << endl;
return 0;
}
Check for overflow: mid*(mid+1LL) uses long long because mid+1LL promotes to long long. Good.
Alternatively, we can compute directly with sqrt:
cpp
ll i = (sqrt(1+8*k)-1)/2;
if (i*(i+1)/2 < k) i++;
But need to ensure sqrt returns double accurately. For k up to 2e9, 8*k up to 1.6e10, sqrt about 126491. So double has 53 bits mantissa, > 4e9? Actually 2e9 requires ~31 bits, but sqrt's integer part is ~126k, so accurate enough. But binary search is simpler.
But careful: In binary search, we set high = n because i cannot exceed n. But what if k is such that the computed i would be > n? But k <= total, and total = n(n+1)/2, so i is at most n. So high=n is fine.
Edge: If k is exactly (i-1)*i/2? Then that would be the last element of previous prefix? But our condition is k <= f(i). Since if k = f(i-1) exactly, then it falls in the previous prefix? Actually if k = f(i-1) = (i-1)i/2, then for i-1, f(i-1) is exactly the last element of prefix i-1. Our condition for i: f(i-1) < k <= f(i). If k equals f(i-1), then it is not > f(i-1), but rather equal. So the element is the last of prefix i-1, not the start of prefix i. So we need to ensure we pick i such that f(i-1) < k <= f(i). In binary search, we are looking for the smallest i such that f(i) >= k. If k = f(i-1), then f(i-1) >= k? Yes, because f(i-1) >= k? Since k = f(i-1), then f(i-1) >= k, so the smallest i with f(i) >= k is i-1? Let's test: Suppose n=3, total=6. k=3, which is f(2)=3. Binary search: low=1, high=3. mid=2: f(2)=3 >=3 => high=2. low=1, high=2, mid=1: f(1)=1 <3 => low=2. low=2 high=2 => i=2. Then prev = (2-1)*2/2 =1. pos = k - prev = 3-1=2. That corresponds to id_2 in the 2nd prefix? But prefix 2 is id1,id2, its second element is id2. But is the 3rd overall element id2? Let's list: S: 1 (prefix1); 1,2 (prefix2) => sequence: 1,1,2,... Actually with n=3, full sequence: prefix1: id1; prefix2: id1, id2; prefix3: id1, id2, id3 => overall: id1, id1, id2, id1, id2, id3. So positions: 1:id1,2:id1,3:id2. So for k=3, answer is id2. Our computed i=2, pos=2 gives id2. Good. So our binary search condition (smallest i with f(i) >= k) works even when k exactly equals f(i-1) because then i-1 satisfies f(i-1) >= k, so the smallest i is i-1. So i becomes i-1, not i. So it's correct.
Thus answer is id_{k - (i-1)i/2} where i = smallest integer with i(i+1)/2 >= k.
But wait: the problem statement says "k ≤ min(2·109, n·(n + 1) / 2)". So k may be up to 2e9 even if n(n+1)/2 > 2e9. So k is at most 2e9. But n can be up to 100k => total up to ~5e9, which is >2e9. So k can be at most 2e9. That's fine.
Implementation in C++ straightforward.
Now note: The problem might be from a contest where participants may think it's more complex, but it's simple.
But double-check: The sequence: each robot says the identifiers of all previous robots and then its own. So it's prefix.
Thus final answer.
But is there any scenario where the identifiers are not in order? No, they are given.
Hence answer.
I'll produce final solution.
But wait, the problem title "Snowball" might be hinting at something else? Possibly there is a twist: The robots are in a line, and they say identifiers in the order: the first says his; the second says the identifier of the first and his own; the third says the identifier of the first, then the second, then his own; ... that's prefix.
Thus we're done.
Now I will produce final answer with explanation.
But I realize I need to be thorough: Sometimes in such problems, the order might be: first robot says his id; second robot says the id of the first, then his own; third robot says the id of the first, then the id of the second, then his own; but also maybe they repeat? Actually that's the same.
Thus final answer.
But there is a subtlety: The problem says "the k-th identifier to be pronounced." We assume pronunciation order as described. Good.
Thus I'll output solution.
But before final, test with more cases:
n=3, k=1 -> id1.
n=3, k=2 -> i=2? f(1)=1<2, f(2)=3>=2 => i=2, prev=1, pos=1 -> id1. Sequence: 1,1,2,... -> second element is id1. Good.
n=3, k=3 -> i=2, prev=1, pos=2 -> id2. Good.
n=3, k=4 -> i=3? f(2)=3<4, f(3)=6>=4 => i=3, prev=3, pos=1 -> id1. Sequence: pos4: after prefix2: positions:1:id1,2:id1,3:id2. Position4 is first element of prefix3: id1. Good.
n=3, k=5 -> i=3, prev=3, pos=2 -> id2.
n=3, k=6 -> i=3, prev=3, pos=3 -> id3.
Works.
Now consider large n and k near total.
Edge: n=100000, k=5000050000? That's total? But k limited to 2e9 so not that large. But if n=100000, total = 5000050000 > 2e9, k can be up to 2e9. So i will be less than n? Possibly i around sqrt(2k) ~ sqrt(4e9)=63245. So i <= n.
Thus binary search works.
Now final code.
Potential pitfalls: Use of int for mid may overflow when mid*(mid+1) even with conversion to long long? In C++, mid is int, but mid*(mid+1LL) promotes to long long because one operand is long long. So safe.
But to avoid any risk, we can cast: (long long)mid * (mid+1) / 2.
We'll do: if (mid * (mid + 1LL) / 2 >= k) . Good.
We should ensure k is long long to avoid overflow in 8*k? But we only use k in comparisons; k is <= 2e9, fits in int, but we can use long long for safety.
Thus final.
Now produce final answer.
```cpp
#include
#include
#include
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
code
int n;
ll k;
cin >> n >> k;
vector<int> id(n);
for (int i = 0; i < n; ++i) {
cin >> id[i];
}
// binary search for the smallest i such that i*(i+1)/2 >= k
int low = 1, high = n;
while (low < high) {
int mid = (low + high) / 2;
if (mid * (mid + 1LL) / 2 >= k) {
high = mid;
} else {
low = mid + 1;
}
}
int i = low; // i is the current prefix length
ll prev = (i - 1LL) * i / 2; // total length before this prefix
int pos = k - prev; // position inside the i-th prefix (1‑based)
cout << id[pos - 1] << endl;
return 0;