void siftdown(vector<int>& nums, int e, int begin, int end){
int i = begin;
int j = 2*begin + 1;
while(j<end){
if(j+1 < end && nums[j+1] > nums[j]){
j++;
}
if(e > nums[j]){
break;
}
nums[i] = nums[j];
i = j;
j = 2*i + 1;
}
nums[i] = e;
}
void heap_sort(vector<int>& nums){
auto end = (int)nums.size();
for(auto k = end/2; k>=0;k--){
siftdown(nums, nums[k], k, end);
auto n = nums;
int i = 0;
}
for(int k = end - 1;k>0;k--){
int e = nums[k];
nums[k] = nums[0];
siftdown(nums, e, 0, k);
}
}
int heap_sort_topk(vector<int>& nums, int topk){
auto len = (int)nums.size();
if(topk > len){
return 0;
}
for ( int k = (len/2); k > 0; k-- ){
sift_down(nums, nums[k], k, len);
}
for( int k = len -1; k > 0; k--){
if ( topk < 0){
return nums[0];
}
sift_down(nums, nums[k], 0, k);
topk--;
}
}