Custom lambda sort O(N) amortized update
\nint n = arr.size();\nnth_element(begin(arr), begin(arr) + (n-1)/2, end(arr));\nint m = arr[(n-1)/2];\nsort(begin(arr), begin(arr) + k, end(arr), [&m] (const a
```\nint n = arr.size();\nnth_element(begin(arr), begin(arr) + (n-1)/2, end(arr));\nint m = arr[(n-1)/2];\nsort(begin(arr), begin(arr) + k, end(arr), [&m] (const auto &i, const auto &j) {\n    if(abs(m - i) != abs(m - j))\n        return abs(m - i) > abs(m - j);\n    else\n        return i > j;\n});\nreturn {begin(arr), begin(arr) + k};\n```\n\nUpdate:\n```nth_element(begin(arr), begin(arr) + k, end(arr), [&m] (const auto &i, const auto &j)```\n\nYou don\'t have to sort the range in order the only requirement is to have all the elements present, *order doesn\'t matter*, so nth_element twice is the best approach, in the contest I used sort for the second operation but I just realized the condition is **not necessary**, nth_element accepts the same binary predicate.
['C']