We need to output the top K largest sums among all contiguous subarray sums of an array A of N positive integers. K is up to 1e5, N up to 1e5. Total subarrays is N(N+1)/2 which can be huge, but we only need top K.
Observation: Since all Ai are positive, the largest subarray sum is the whole array sum. The next largest would be either the whole array minus a small element from one of the ends? Actually because all positives, the subarray sums are larger when they include more elements. But the sorted list in non-increasing order: basically the subarray sums sorted descending.
Goal: generate the largest K sums efficiently.
We can think of all subarray sums as sums of intervals [l, r] with 0-indexed? Let's define prefix sums: P[0]=0, P[i]=A[0]+...+A[i-1]. Then sum(l,r) = P[r+1] - P[l] for 0 ≤ l ≤ r < N.
We need top K values of P[r+1] - P[l] for l ≤ r.
Since all Ai positive, P is strictly increasing. So P[0]=0 < P[1] < ... < P[N].
Then the sum of subarray [l, r] = P[r+1] - P[l].
We need the largest K differences where the "right" index (r+1) > "left" index (l). Since P increasing, largest differences occur when l is small and r+1 is large. The maximum is P[N] - P[0] = total sum.
We need next largest.
This is reminiscent of the problem: given two sorted lists, find top K sums from pairs (i, j) where i < j and value = P[j] - P[i]. But note i and j indices with i < j. The set of all pairs (i, j) with i < j. P is increasing. So we need largest K differences.
We could consider each i as a "left" index, then for each i, the differences with j > i are decreasing as j increases? Actually P[j] increasing, so P[j] - P[i] increases as j increases. For fixed i, the largest difference is with j = N (largest j) => sum of subarray from i to N-1. That is the suffix starting at i. So each left index i yields a sorted list of suffix sums: from i to N-1, i to N-2, ...? Actually for fixed i, as r decreases (i.e., j = r+1 decreases), the sum decreases. But we want descending order overall.
We can treat it as: we have N "left" indices i=0..N-1. For each i, there is a set of possible sums S_i = { P[j] - P[i] for j = i+1 .. N } which are decreasing as j decreases. The largest among all S_i is total sum with i=0, j=N.
Goal: merge these N sorted descending lists (each list is sums for a given i, sorted descending as j increases? Wait: For fixed i, if we consider j from i+1 to N, P[j] - P[i] increases with j. So if we want descending order, we need to go from high j to low j: i.e., the list in descending order is: sum_i,N = P[N] - P[i] (largest), then P[N-1]-P[i], ..., P[i+1]-P[i] (smallest). So each list is descending. And we need top K from the union of these lists.
We can use a max-heap: initially insert for each i the largest sum from that i, i.e., suffix sum from i to end: total - prefix sum up to i-1? Actually sum of subarray starting at i: P[N] - P[i]. So we push (value = P[N] - P[i], i, current_j = N). Then we repeatedly pop the max, output it, and then for that i, we can push the next smaller sum from the same i, i.e., decrease j by 1 (i.e., next lower j) as long as j > i (i.e., still valid). Specifically, if we popped (value = P[j] - P[i]), then next candidate for same i is P[j-1] - P[i] (since decreasing j gives next smaller sum). So we need to track the current j index (the right endpoint + 1) for that i. Initially j = N. After popping, we set j = j-1 and if j > i, push new value.
This is similar to merging K sorted lists, where each list corresponds to a start index. There are N lists. But we only need top K elements. The total push/pop operations will be O(K log N) because we push at most K+N times? Actually initial N pushes, then each pop may push one new element, so total pushes ≤ N + K, pops = K. So O((N+K) log N) which is fine: N up to 1e5, K up to 1e5 => ~2e5 log 1e5.
But careful: The lists are not independent because the same sum might appear from different intervals? Possibly duplicates. But the problem expects output all K including duplicates. So algorithm should output exactly the order, duplicates allowed.
We must verify that this method indeed produces the descending sorted order of all subarray sums. It is essentially a standard algorithm to generate sorted order of all sums of the form P[j] - P[i] for i<j, given sorted P. Because for each i, the sequence for decreasing j is descending. And we are merging descending sequences with a max-heap. So yes, it will output in descending order.
But caution: Are there any pitfalls? For a fixed i, the sequence P[j] - P[i] is strictly decreasing as j decreases because P is strictly increasing (Ai positive). So yes, it's strictly decreasing.
Thus algorithm:
- Compute prefix sums P[0..N] where P[0]=0, P[i]=P[i-1]+A[i-1].
- Create a max-heap of tuples (value, i, j) where j is the current right index (meaning sum = P[j] - P[i]). We'll push for each i from 0 to N-1: value = P[N] - P[i], i, j = N.
- Then for k=1..K:
- pop max (val, i, j)
- output val
- if j-1 > i: push (P[j-1] - P[i], i, j-1)
This yields top K sums.
Check example:
N=3, A=[1,3,4], P=[0,1,4,8]
i=0: P[3]-P[0]=8
i=1: P[3]-P[1]=8-1=7? Actually P[3]=8, P[1]=1 =>7
i=2: P[3]-P[2]=8-4=4
Initial heap: (8,0,3), (7,1,3), (4,2,3)
pop 8 -> output 8; then push for i=0, j=2: P[2]-P[0]=4-0=4? Wait P[2]=4, sum=4.
Now heap: (7,1,3), (4,2,3), (4,0,2)
pop 7 -> output 7; push for i=1, j=2: P[2]-P[1]=4-1=3 (but j=2>i=1? yes, 2>1 so valid). So push (3,1,2)
Heap now: (4,2,3), (4,0,2), (3,1,2)
pop 4 (maybe from i=2) -> output 4; push for i=2, j=2? Actually j=3-> j-1=2, but j-1 =2 which equals i=2? condition j-1 > i? j=3, i=2: j-1=2, not > i (2>2 false) so no push. So after pop, no push.
Heap: (4,0,2), (3,1,2)
pop 4 (from i=0) -> output 4; push for i=0, j=1: P[1]-P[0]=1-0=1 (j-1=1, > i=0? yes 1>0). push (1,0,1)
Heap: (3,1,2), (1,0,1)
Next pop 3 -> output 3, but K=4 we already have outputs: 8,7,4,4. So we stop. The algorithm gave 8,7,4,4 which matches.
Second example: N=3, A=[10,2,7]; P=[0,10,12,19]
Initial:
i0:19-0=19 (j=3)
i1:19-10=9? Actually P[3]=19, P[1]=10 ->9
i2:19-12=7
heap: (19,0,3), (9,1,3), (7,2,3)
pop19-> output19; push i0 j=2: P[2]-P[0]=12-0=12 ->push (12,0,2)
heap: (12,0,2), (9,1,3), (7,2,3)
pop12-> output12; push i0 j=1: P[1]-P[0]=10-0=10 (j=1>i=0? yes) push (10,0,1)
heap: (10,0,1), (9,1,3), (7,2,3)
pop10-> output10; (K=3 so stop). Outputs 19,12,10 matches.
Thus algorithm works.
Now constraints: N up to 1e5, K up to 1e5, Ai up to 1e9, so sums up to 1e14, fits in 64-bit.
Complexity: O(N + K log N) time, O(N) extra.
Implementation details:
-
Use 0-indexed arrays.
-
Compute prefix sums as long long (or 64-bit).
-
Use a max-heap: in C++ use priority_queue of tuple; need to define custom comparator or use negative values for min-heap? Better use priority_queue with a struct containing value, i, j; and operator< for max. But by default priority_queue is max for the first element if we use less? Actually default is max for the type; but if we use tuple<int64_t,int,int>, it compares lexicographically. That's okay if we want max based on value first. But we need to ensure that when values tie, any order works because we just need correct top K order. However duplicates may be output in any order as long as overall descending? Actually if there are ties, the order among equal values is not important because we just output the values. The problem statement: "first K elements from the list L" where L is sorted non-increasing. For equal values, any order among them is fine because they are equal. However careful: In merging multiple lists, if we have ties, the heap could output one before another, but they are equal values, so output order among equal values doesn't matter as long as they are in non-increasing order. Since they are equal, it's fine. So we can just compare by value; in case of tie, we don't care.
-
But we need to store j (the right index). For each i, we need to know the next j. So we can store j.
Alternatively, we can store for each i the current right index and just compute value from prefix sums on the fly. But that would require recomputing each time, but computing difference is cheap. So we can store (value, i, j). Or store (value, i, j) and when popping, push new value by recomputing P[j-1]-P[i].
Implementation steps:
- Read N, K.
- Read array A.
- Compute prefix sums P of size N+1: P[0]=0; for i=1..N: P[i] = P[i-1] + A[i-1].
- Define struct Item { long long val; int i; int j; }. For priority_queue, define operator< such that we want max on val. So we can define:
struct Item {
long long val;
int i, j;
bool operator<(const Item& other) const {
return val < other.val; // for max-heap? Actually priority_queue uses std::less by default, which yields max if operator< returns true for lower? Wait: In C++ priority_queue: by default it's a max-heap using std::less, meaning the largest element is at top. The comparison function used is Compare, which should return true if the first argument is less than the second (i.e., a < b) for max-heap? Actually typical: template<class T, class Container = vector, class Compare = less>, then the element that is "largest" according to Compare is at the top. With less, it uses operator<; the largest element is the one that would come last in a sorted ascending order, so top is greatest. So if we define operator< as returning true when val < other.val, then the element with larger val will be considered "greater"? Let's test: If we have a < b, then a is less than b. In the heap, the top is the element that is not less than any other; i.e., the maximum. So if we want max-heap based on val, we can just use default less, which uses operator< where smaller val is less. That yields max. But to make it max explicitly, we can define:
bool operator<(const Item& other) const { return val < other.val; }
But this will order by val ascending? Actually when we insert items, the priority_queue's ordering uses the comparison to maintain the largest at top. The comparison function returns true if the first argument is considered to be less than the second. So with less, it uses operator<; if we have a.val = 5, b.val=10, then a < b true, so a is less than b. Then the top will be the greatest element, which is b. So that's max-heap. So if we define operator< based on val, it's fine.
But we need to be careful: if we want max, we could also define custom comparator: greater for min-heap but we want max. So using default less and having val as first element in tuple works. Alternatively, use std::tuple<long long, int, int> and push (-val) for min-heap? Simpler: use tuple with negative? I'll just define struct with operator< as above.
But note: priority_queue will use operator< defined. So ensure it's correct.
- Create priority_queue pq.
- For i from 0 to N-1:
value = P[N] - P[i];
pq.push(Item{value, i, N});
- For step in 0..K-1:
item = pq.top(); pq.pop();
output item.val (space separated).
if (item.j - 1 > item.i) { // equivalent to item.j > item.i+1
long long new_val = P[item.j - 1] - P[item.i];
pq.push(Item{new_val, item.i, item.j - 1});
}
Edge Cases:
- K may equal total number of subarrays, which is N*(N+1)/2. In that case we output all sums. Our algorithm will eventually push all possible sums? Initially push N items. Each pop for each i will eventually push all remaining sums for that i. Total pushes = total subarrays = N(N+1)/2. That could be large if N=1e5 and K=~5e9, but K is limited to min(total, 1e5). So we will not push more than N + K items, because we only push after pops up to K times. Actually careful: if K equals total, we would push all subarrays. But total can be huge (5e9), exceeding our computational limit. However constraints: K ≤ min(total, 1e5). So K is at most 1e5. So we only push at most N + K items, not total. That's fine because we stop after K pops; we don't need to push further items beyond those needed to generate the top K. But is it guaranteed that the top K items are generated from at most K pushes after initial? For each pop we push at most one new item. So after K pops, we will have pushed at most K additional items, so total heap size O(N+K). So we don't generate all subarrays, only the ones along the frontier of merging. This is correct because the merging process only requires generating the next item from a list when its current top is popped. Since we only need the top K overall, we only need to explore the lists up to the point that we have extracted K items. That is exactly how the K-way merge works.
But is there any scenario where we might need to push more than K items to guarantee the top K order? In merging sorted lists, to get the first K, we only need to pop K times, and each pop may lead to a push from that same list. So yes, at most K pushes after initial N. So total O(N+K).
One nuance: The lists are not independent in the sense that the same sum might appear multiple times, but that doesn't affect correctness.
So algorithm is efficient.
But we must ensure that using prefix sums and differences works without overflow: Ai up to 1e9, N up to 1e5, so total sum up to 1e14, fits in 64-bit signed (9e18). Use long long.
Now let's test on some random cases to verify. We'll maybe implement a brute for small N to test.
Potential pitfalls:
- The condition item.j - 1 > item.i: Actually j is the index in prefix sums representing the right endpoint +1. For subarray [i, j-1] (since i is left index, j is right+1). So valid subarray requires j >= i+1. So j-1 > i is equivalent to j > i+1. Another way: if after popping with j, we want next sum with same i but smaller right endpoint: the new j' = j-1 must satisfy j' >= i+1 (i.e., j-1 >= i+1 => j >= i+2). Actually we need to ensure that there is still at least one element in subarray: j-1 >= i+1? Wait: For fixed i, the possible j range from i+1 to N. So j must be at least i+1. Initially we push with j = N (largest). After popping, we consider j-1. If j-1 >= i+1, then it's valid. So condition: j-1 >= i+1 => j >= i+2. Equivalent to j > i+1. So we can check: if (item.j > item.i + 1) then push. That's equivalent.
In code we wrote if (item.j - 1 > item.i). Yes that's same as item.j > item.i + 1.
So all good.
Now we need to output K numbers space-separated.
Edge Cases: N=1. Then total subarrays =1, K is 1. P=[0, A0]. For i=0: val=P[1]-P[0]=A0. push. pop -> output A0. Then check j=1, j-1=0 not > i=0, so no push. Works.
Another: N=5, K small.
We can test with all positives.
But what about duplicates? Example earlier had duplicates. Output order of duplicates: as they appear from heap, they may be interleaved. But since equal, it's fine.
Now, what about memory? Storing prefix sums of N+1 longs: OK. Priority queue of size up to N + (something). N up to 1e5, so fine.
Thus algorithm is solid.
But wait: Could there be a more efficient approach? Not needed.
Now let's double-check the sorted order. Since the lists are descending, merging via max-heap yields correct descending order.
But we need to be careful: For a fixed i, the sums are strictly decreasing? Because P is strictly increasing, so P[j] - P[i] strictly decreasing as j decreases. So each list is strictly decreasing. Good.
Potential issue: When we pop an item (value, i, j), we push the next item from that list: (P[j-1] - P[i], i, j-1). But what about the scenario where there are multiple lists and the next largest might be from the same i but j-1 vs from another list's current top? The heap handles that.
Thus the solution seems correct.
Now we should consider if there is any possibility of generating the same sum from two different (i,j) pairs? That's fine. The heap will contain duplicates. When popping, if two items have same value, whichever is popped first, it's okay. And the subsequent push might be the same value again from another list, but again it's okay.
Now implement.
Implementation in C++:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Item {
ll val;
int i;
int j;
bool operator<(const Item& other) const {
return val < other.val; // max-heap based on val
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, K;
cin >> N >> K;
vector A(N);
for (int i=0;i<N;++i) cin >> A[i];
vector P(N+1);
P[0] = 0;
for (int i=1;i<=N;++i) P[i] = P[i-1] + A[i-1];
priority_queue<Item> pq;
for (int i=0;i<N;++i) {
pq.push(Item{P[N] - P[i], i, N});
}
vector<ll> res;
res.reserve(K);
for (int step=0;step<K;++step) {
Item cur = pq.top(); pq.pop();
res.push_back(cur.val);
if (cur.j > cur.i + 1) { // equivalent to cur.j - 1 >= cur.i + 1
ll new_val = P[cur.j - 1] - P[cur.i];
pq.push(Item{new_val, cur.i, cur.j - 1});
}
}
for (int i=0;i<K;++i) {
if (i>0) cout << ' ';
cout << res[i];
}
cout << endl;
return 0;
}
Now, complexity O((N+K) log N). N=1e5, K=1e5, log N ~17, fine.
But could there be any memory overhead? storing A and P and heap.
Alternative: we might not need to store A after computing P; we can compute P directly. Fine.
Now I should consider if there's any scenario where the algorithm fails due to integer overflow? P up to 1e14, differences within same range. long long is at least 64-bit, up to 9e18, safe.
Now let's test with some random cases using brute for small N.
But since we are reasoning, it's okay.
One more nuance: The problem says "sorted (in non-increasing order) list of size N*(N+1)/2 containing the sums of all possible contiguous subarrays of A". If all Ai are positive, indeed sums are positive and the descending order is as described. But if there were negative numbers, it would be different. However constraints say Ai are positive integers, so no issues.
Thus solution accepted.
But wait: The constraints also allow Ai up to 1e9, positive. So all good.
Now, verify with another example: N=4, A=[2,2,2,2]. Total subarray sums: length 1: 2,2,2,2; length2:4,4,4; length3:6,6; length4:8. Sorted non-increasing: 8,6,6,4,4,4,2,2,2,2. Let's see our algorithm.
P=[0,2,4,6,8].
Initial: i0:8-0=8 (j=4)
i1:8-2=6 (j=4)
i2:8-4=4 (j=4)
i3:8-6=2 (j=4)
heap: (8,0,4),(6,1,4),(4,2,4),(2,3,4)
pop8 output8; push i0 j=3: P[3]-P[0]=6 ->(6,0,3)
heap: (6,1,4),(6,0,3),(4,2,4),(2,3,4)
pop6 (maybe from i1) output6; push i1 j=3: P[3]-P[1]=6-2=4 ->(4,1,3)
heap: (6,0,3),(4,2,4),(4,1,3),(2,3,4)
pop6 (i0) output6; push i0 j=2: P[2]-P[0]=4 ->(4,0,2)
heap: (4,2,4),(4,1,3),(4,0,2),(2,3,4)
pop4 (any) output4; suppose from i2. push i2 j=3? i2=2, j=4-> j-1=3 >= i+1=3? Actually i2=2, j=4 -> j-1=3, condition j > i+1? 4 > 3? yes, 4>3 => push P[3]-P[2]=6-4=2 ->(2,2,3)
heap now: (4,1,3),(4,0,2),(2,3,4),(2,2,3)
pop4 output4; push from i1 j=3? Actually popped from i1? whichever. Continue. The order of 4's and 2's may intermix. But first K for K= say 6 would be 8,6,6,4,4,4. That matches.
Thus algorithm works.
Now, confirm that we handle K up to total subarrays but limited to 1e5. Since K ≤ 1e5, it's fine.
One potential optimization: Use 0-indexed but we can also use 1-indexed for simplicity. We'll adopt 0-indexed as above.
Now, I'll consider if there's a more subtle issue: Because each list is descending, and we are merging them using a max-heap, the order we produce is exactly the sorted descending order. This is a classic algorithm to find top K from N sorted lists. So it's correct.
Thus the answer is the described approach.
Now, we need to provide the solution in a response format? The user is asking: "Chef likes arrays a lot. Today, he found ... Can you help him in accomplishing this task?" This is a problem statement. The user likely expects me to provide a solution, possibly code? But the context is that they may be asking for explanation or code. As ChatGPT, I should provide an explanation and possibly code. The instruction "You are an expert in competitive programming" suggests I should provide a solution. However the problem statement seems like a typical coding problem; the user might be asking for help in solving it. So I'll produce a detailed explanation and then the code.
But careful: The user might be expecting just the output? No, they ask: "Can you help him in accomplishing this task?" So I can respond with a solution approach and code.
I'll structure my answer:
- Explanation of the problem: need to output K largest subarray sums.
- Observation: all numbers positive => prefix sums increasing.
- Represent each start index i, the subarray sums ending at j (j>i) in descending order (as j decreases).
- Use a max-heap to merge these N lists, similar to merging K sorted lists.
- Algorithm steps.
- Complexity analysis.
- Provide C++ code.
I'll ensure the code is correct and efficient.
Now, check potential pitfalls: Use of long long; use of i and j as int. N up to 1e5, so int is fine.
Edge Cases: K could be 0? But K>=1 per constraints. So no.
Now, might there be any time limit issues? O((N+K) log N) is fine.
Now, final answer.
But before final, let's verify with a more extensive random test via mental simulation or we could quickly reason with a small N random, but I trust the algorithm.
One nuance: When we push initial N items, we push for i from 0 to N-1. But note that for i=N-1, the only possible j is N (since i+1 = N). So that's valid. So initial push includes all suffix sums from each start.
Now, if K is large, we might eventually push all possible sums for some i up to small j. That's fine.
Now, some might think about using a min-heap and maintaining size K, but that would be less efficient for K up to 1e5 and total sums huge? Actually we could iterate all subarrays? No.
Now, alternative: Since Ai positive, the largest sums are basically the sums of large subarrays. Could there be a more direct way? Possibly using a priority queue as we did.
Thus answer.
Now, I'll produce final answer. We need to output the K largest sums among all contiguous subarrays of an array A of N positive integers.
N ≤ 10⁵, K ≤ min(N(N+1)/2, 10⁵).
Since all numbers are positive, the sum of a subarray increases when we extend it to the right. This property allows an efficient solution using prefix sums and a max-heap to merge N sorted descending lists.
Key Observation
Let P[0]=0 and P[i]=A[0]+…+A[i-1] for i=1..N.
The sum of subarray [l, r] (0‑based) is P[r+1] - P[l].
For a fixed start index l, as the end index r increases, the sum increases.
Thus, for each l we obtain a sorted descending list by taking r from N-1 down to l:
S_l = { P[N]-P[l], P[N-1]-P[l], …, P[l+1]-P[l] }
We have N such lists, each in descending order.
The union of all S_l is exactly the set of all subarray sums.
Algorithm: K-way Merge with a Max‑Heap
- Compute prefix sums
P[0..N].
- Initialize a max‑heap where each entry is
(value, l, r_idx).
value = P[r_idx] - P[l]
r_idx is the right endpoint + 1 (r_idx = r+1), ranging from l+1 to N.
- For every start index
l = 0 … N-1, push the largest sum of its list:
value = P[N] - P[l], with r_idx = N.
- Repeat K times:
- Pop the heap’s top (largest
value). Output it.
- For that same
l, if r_idx - 1 is still at least l+1 (i.e., r_idx > l+1), push the next sum from the list:
new_value = P[r_idx-1] - P[l] with r_idx-1.
- Stop after K outputs.
The heap never contains more than N + K elements, and each operation is O(log N).
Complexity
- Time: O((N+K) log N) — computing prefix sums O(N), heap operations O((N+K) log N).
- Space: O(N) for prefix sums + O(N+K) for the heap.
Both are easily within limits for N, K ≤ 10⁵.
Correctness
Because each list S_l is strictly decreasing (prefix sums are strictly increasing), extracting the current maximum from the union of all lists via a max‑heap produces the globally largest remaining sum. When we remove a sum P[j]-P[l], the next smaller sum from the same l is P[j-1]-P[l]. Pushing it back keeps the heap correctly representing the frontier of all lists. After K steps we have obtained the K largest sums in non‑increasing order.
C++ Implementation
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Item {
ll val; // P[j] - P[i]
int i; // start index (0‑based)
int j; // right endpoint + 1 (j = r+1)
bool operator<(const Item& other) const {
// max‑heap: larger value has higher priority
return val < other.val;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, K;
cin >> N >> K;
vector<ll> A(N);
for (int i = 0; i < N; ++i) cin >> A[i];
// prefix sums
vector<ll> P(N + 1);
for (int i = 1; i <= N; ++i) P[i] = P[i - 1] + A[i - 1];
priority_queue<Item> pq;
for (int i = 0; i < N; ++i) {
// initial element: subarray [i, N-1]
pq.push(Item{P[N] - P[i], i, N});
}
vector<ll> ans;
ans.reserve(K);
while ((int)ans.size() < K) {
Item cur = pq.top(); pq.pop();
ans.push_back(cur.val);
// if there is a next (shorter) subarray starting at i
if (cur.j > cur.i + 1) {
ll new_val = P[cur.j - 1] - P[cur.i];
pq.push(Item{new_val, cur.i, cur.j - 1});
}
}
for (int i = 0; i < K; ++i) {
if (i) cout << ' ';
cout << ans[i];
}
cout << '\n';
return 0;
}
Example
Input 1
3 4
1 3 4
Prefix: [0,1,4,8]
Initial heap: (8,0,3), (7,1,3), (4,2,3)
Steps:
- Pop 8 → output 8; push
(4,0,2)
- Pop 7 → output 7; push
(3,1,2)
- Pop 4 → output 4; (i=2 gives no push)
- Pop 4 → output 4;
Output: 8 7 4 4 ✅
Input 2
3 3
10 2 7
Prefix: [0,10,12,19]
Initial heap: (19,0,3), (9,1,3), (7,2,3)
Steps:
- Pop 19 → output 19; push
(12,0,2)
- Pop 12 → output 12; push
(10,0,1)
- Pop 10 → output 10;
Output: 19 12 10 ✅
The algorithm works for all valid inputs.