Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
// #include<ext/rope>
// using namespace __gnu_cxx
// #include<ext/pb_ds/priority_queue.hpp>
// using namespace __gnu_pbds;
#define lowbit(x) (x & -x)
#define pb push_back
#define empb emplace_back
#define all(x) (x).begin(), (x).end()
#define clr(a, b) memset(a, b, sizeof(a))
#define caze(T) for (cin >> T; T; T--)
#define inf (1 << 29)
#define Endl ('\n')
#define fi first
#define se second
#define db double
#define ldb long double
#define ll long long
#define pii pair<int, int>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define ull unsigned long long
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
inline char nc() {
#define SZ 1000000
  static char buf[SZ], *p1, *p2;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, SZ, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
template <typename T> inline int scan(T &x) {
  char c, sign = 1;
  while (!isdigit(c = nc()) && c != EOF)
    if (c == '-')
      sign *= -1;
  if (c == EOF)
    return EOF;
  for (x = 0; isdigit(c); c = nc())
    x = (x << 1) + (x << 3) + (c & 15);
  return x *= sign, 1;
}
template <typename T, typename... Args> inline int scan(T &x, Args &...args) {
  return scan(x), scan(args...);
}
int main() {
  IOS;
  int n, k;
  cin >> n >> k;
  vector<int> v[2];
  int c[2] = {0, 0};
  v[0].pb(0);
  v[1].pb(0);
  for (int i = 0, x; i < n; ++i) {
    cin >> x;
    int t = x < 0 ? 0 : 1;
    if (!x) {
      --k;
      continue;
    }
    x = abs(x);
    c[t]++;
    v[t].pb(x);
  }
  sort(all(v[0]));
  int ans = 1 << 30;
  for (int i = 0; i <= min(c[0], k); ++i)
    ans = min(ans, v[0][i] + v[1][k - i] + min(v[0][i], v[1][k - i]));
  cout << ans << Endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// #include<ext/rope>
// using namespace __gnu_cxx
// #include<ext/pb_ds/priority_queue.hpp>
// using namespace __gnu_pbds;
#define lowbit(x) (x & -x)
#define pb push_back
#define empb emplace_back
#define all(x) (x).begin(), (x).end()
#define clr(a, b) memset(a, b, sizeof(a))
#define caze(T) for (cin >> T; T; T--)
#define inf (1 << 29)
#define Endl ('\n')
#define fi first
#define se second
#define db double
#define ldb long double
#define ll long long
#define pii pair<int, int>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define ull unsigned long long
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
inline char nc() {
#define SZ 1000000
  static char buf[SZ], *p1, *p2;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, SZ, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
template <typename T> inline int scan(T &x) {
  char c, sign = 1;
  while (!isdigit(c = nc()) && c != EOF)
    if (c == '-')
      sign *= -1;
  if (c == EOF)
    return EOF;
  for (x = 0; isdigit(c); c = nc())
    x = (x << 1) + (x << 3) + (c & 15);
  return x *= sign, 1;
}
template <typename T, typename... Args> inline int scan(T &x, Args &...args) {
  return scan(x), scan(args...);
}
int main() {
  IOS;
  int n, k;
  cin >> n >> k;
  vector<int> v[2];
  int c[2] = {0, 0};
  v[0].pb(0);
  v[1].pb(0);
  for (int i = 0, x; i < n; ++i) {
    cin >> x;
    int t = x < 0 ? 0 : 1;
    if (!x) {
      --k;
      continue;
    }
    x = abs(x);
    c[t]++;
    v[t].pb(x);
  }
  sort(all(v[0]));
  int ans = 1 << 30;
  for (int i = 0; i <= min(c[0], k); ++i)
    if (k - i <= c[1])
      ans = min(ans, v[0][i] + v[1][k - i] + min(v[0][i], v[1][k - i]));
  cout << ans << Endl;
}