Language: C++

#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <deque>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define int int64_t
#define pb push_back
#define pbb pop_back
#define pf pop_front
#define pff push_front
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const int INF = 1e12, MOD = 1e9 + 7, MAXN = 1e5 + 7, MAX = 5;
const ld EPS = 1e-9, PI = acos(-1);

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed;
    cout.precision(10);

    int n, k;
    cin >> n >> k;
    vector<int> nums(n);
    for (int i = 0; i < n; ++i)
        cin >> nums[i];

    vector<int> neg, pos;
    for (int i = 0; i < n; ++i) {
        if (nums[i] < 0)
            neg.pb(nums[i]);
        else if (nums[i] == 0)
            k--;
        else
            pos.pb(nums[i]);
    }

    if (k == 0) {
        cout << 0 << endl;
        return 0;
    }

    int ans = INF;
    if (neg.size() >= k)
        ans = min(ans, abs(neg[neg.size() - k]));
    if (pos.size() >= k)
        ans = min(ans, pos[k - 1]);
    for (int now = max(0LL, (int)(neg.size() - k + 1)); now < neg.size() && k - (neg.size() - now) < pos.size(); ++now) {
        //cout << now << " " << k - (neg.size() - now) - 1 << " " << neg[now] << " " << pos[k - (neg.size() - now) - 1] << endl;
        ans = min(ans, abs(neg[now]) + pos[k - (neg.size() - now) - 1] + min(abs(neg[now]), pos[k - (neg.size() - now) - 1]));
    }

    cout << ans << endl;

    return 0;
}


Compiler output:
a.cc: In function 'int32_t main()':
a.cc:63:23: error: no matching function for call to 'max(long long int, int64_t)'
   63 |     for (int now = max(0LL, (int)(neg.size() - k + 1)); now < neg.size() && k - (neg.size() - now) < pos.size(); ++now) {
      |                    ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:63:23: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int64_t' {aka 'long int'})
   63 |     for (int now = max(0LL, (int)(neg.size() - k + 1)); now < neg.size() && k - (neg.size() - now) < pos.size(); ++now) {
      |                    ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:9:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:63:23: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   63 |     for (int now = max(0LL, (int)(neg.size() - k + 1)); now < neg.size() && k - (neg.size() - now) < pos.size(); ++now) {
      |                    ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~