Problem p03276 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
// #include <bits/extc++.h>
using namespace std;

#define debug(s) cout << #s << " = " << s << endl
#define all(v) (v).begin(), (v).end()
#define KeepUnique(v) (v).erase(unique(all(v)), v.end())
#define MEMSET(a, val) memset(a, val, sizeof(a))
#define PB push_back
#define endl '\n'
typedef long long ll;

inline int myrand(int l, int r) {
  int ret = rand();
  ret <<= 15;
  ret ^= rand();
  if (ret < 0)
    ret = -ret;
  ret %= (r - l + 1);
  ret += l;
  return ret;
}

template <typename F, typename S>
ostream &operator<<(ostream &os, const pair<F, S> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}

typedef pair<int, int> ii;

template <typename T>
using min_pq = std::priority_queue<T, vector<T>, greater<T>>;

// int dx[] = {-1, +0, +1, +0};
// int dy[] = {+0, +1, +0, -1};

int32_t main() {
  // freopen("in", "r", stdin);
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int &b : v)
    cin >> b;
  vector<int> L, R;
  for (int i = 0; i < n; i++)
    if (v[i] >= 0)
      R.emplace_back(v[i]);
  for (int i = n - 1; i >= 0; i--)
    if (v[i] < 0)
      L.emplace_back(-v[i]);
  if (L.size() == 0) {
    cout << R[k - 1] << endl;
  } else if (R.size() == 0) {
    cout << L[k - 1] << endl;
  } else {
    // for(int b : L) cout << b << ' '; cout << endl;
    // for(int b : R) cout << b << ' '; cout << endl;
    int mn = 1e9;
    if (L.size() >= k)
      mn = min(mn, L[k - 1]);
    if (R.size() >= k)
      mn = min(mn, R[k - 1]);
    for (int Left = 1, Right = k - 1; Left < k; Left++, Right--) {
      mn = min(
          {mn, 2 * L[Left - 1] + R[Right - 1], 2 * R[Right - 1] + L[Left - 1]});
    }
    cout << mn << endl;
  }
}

Fixed submission:
// #pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
// #include <bits/extc++.h>
using namespace std;

#define debug(s) cout << #s << " = " << s << endl
#define all(v) (v).begin(), (v).end()
#define KeepUnique(v) (v).erase(unique(all(v)), v.end())
#define MEMSET(a, val) memset(a, val, sizeof(a))
#define PB push_back
#define endl '\n'
typedef long long ll;

inline int myrand(int l, int r) {
  int ret = rand();
  ret <<= 15;
  ret ^= rand();
  if (ret < 0)
    ret = -ret;
  ret %= (r - l + 1);
  ret += l;
  return ret;
}

template <typename F, typename S>
ostream &operator<<(ostream &os, const pair<F, S> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}

typedef pair<int, int> ii;

template <typename T>
using min_pq = std::priority_queue<T, vector<T>, greater<T>>;

// int dx[] = {-1, +0, +1, +0};
// int dy[] = {+0, +1, +0, -1};

int32_t main() {
  // freopen("in", "r", stdin);
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int &b : v)
    cin >> b;
  vector<int> L, R;
  for (int i = 0; i < n; i++)
    if (v[i] >= 0)
      R.emplace_back(v[i]);
  for (int i = n - 1; i >= 0; i--)
    if (v[i] < 0)
      L.emplace_back(-v[i]);
  if (L.size() == 0) {
    cout << R[k - 1] << endl;
  } else if (R.size() == 0) {
    cout << L[k - 1] << endl;
  } else {
    // for(int b : L) cout << b << ' '; cout << endl;
    // for(int b : R) cout << b << ' '; cout << endl;
    int mn = 1e9;
    if (L.size() >= k)
      mn = min(mn, L[k - 1]);
    if (R.size() >= k)
      mn = min(mn, R[k - 1]);
    for (int Left = 1, Right = k - 1; Left < k; Left++, Right--) {
      if (L.size() < Left or R.size() < Right)
        continue;
      mn = min(
          {mn, 2 * L[Left - 1] + R[Right - 1], 2 * R[Right - 1] + L[Left - 1]});
    }
    cout << mn << endl;
  }
}