Language: C++

#include <iostream>
#include <string>
#include <set>
#include <vector>
#include <stack>
#include <queue>
using namespace std;

int main(){

    int N, K;
    int tmp;

    cin >> N >> K;

    if( K == 1){
        tmp = 0;
        cout << tmp << endl;
        return 0;
    }

    vector<int> x(N);
    vector<int> distance(N);
    vector<int> uniqueX(N);
    vector<int>::iterator itr;
    vector<int> number(N);
    set<int> backet;

    for(int i = 0; i < N; ++i) {
        cin >> tmp;
        x[i] = tmp;
    }


    for(int i = 0; i < N; ++i){
        tmp = abs( x[i] ); 
        distance[i] = tmp;
        uniqueX[i] = tmp;
    }

    sort(uniqueX.begin(), uniqueX.end());
    uniqueX.erase(unique(uniqueX.begin(), uniqueX.end()), uniqueX.end());

    //for(int i =0; i < uniqueX.size(); ++i) cout << uniqueX[i] << endl;
    for(int i = 0; i < N; ++i){
        itr = find(uniqueX.begin(), uniqueX.end(), distance[i]);
        number[i] = itr - uniqueX.begin() + 1;
    }

    //for(int i =0; i < number.size(); ++i) cout << number[i] << endl; 

    int minnum = 1;
    int Kflag = 0;
    for(int k = 1; k < K; ++k){
        for(int i = 0; i < number.size(); ++i){
            if(backet.count(i)) continue;
            if(number[i] == k){
                backet.insert(i);
                Kflag++;
            }
            if(Kflag == K) break;
        }
        if(Kflag == K) break;
    }

    vector<int> backet_vec(backet.begin(), backet.end());
    sort(backet_vec.begin(), backet_vec.end(),greater<int>());

    //cout << backet_vec.back() << endl;
    //cout << backet_vec.front() << endl; 
    int maxi = backet_vec.front();
    int mini = backet_vec.back(); 

    if(distance[maxi] >= distance[mini]){
        cout << distance[mini] * 2 + distance[maxi] << endl; 
        return 0;
    }
    if(distance[mini] > distance[maxi]){
        cout << distance[maxi] * 2 + distance[mini] << endl; 
        return 0;
    }
}

Compiler output:
a.cc: In function 'int main()':
a.cc:41:5: error: 'sort' was not declared in this scope; did you mean 'short'?
   41 |     sort(uniqueX.begin(), uniqueX.end());
      |     ^~~~
      |     short
a.cc:42:19: error: 'unique' was not declared in this scope; did you mean 'uniqueX'?
   42 |     uniqueX.erase(unique(uniqueX.begin(), uniqueX.end()), uniqueX.end());
      |                   ^~~~~~
      |                   uniqueX
a.cc:46:19: error: no matching function for call to 'find(std::vector<int>::iterator, std::vector<int>::iterator, __gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&)'
   46 |         itr = find(uniqueX.begin(), uniqueX.end(), distance[i]);
      |               ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/bits/locale_facets.h:48,
                 from /usr/include/c++/14/bits/basic_ios.h:37,
                 from /usr/include/c++/14/ios:46,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/streambuf_iterator.h:435:5: note: candidate: 'template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT> >::__type std::find(istreambuf_iterator<_CharT>, istreambuf_iterator<_CharT>, const _CharT2&)'
  435 |     find(istreambuf_iterator<_CharT> __first,
      |     ^~~~
/usr/include/c++/14/bits/streambuf_iterator.h:435:5: note:   template argument deduction/substitution failed:
a.cc:46:19: note:   '__gnu_cxx::__normal_iterator<int*, std::vector<int> >' is not derived from 'std::istreambuf_iterator<_CharT>'
   46 |         itr = find(uniqueX.begin(), uniqueX.end(), distance[i]);
      |               ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~