Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fo(a, b) for (int a = 0; a < b; a++)
#define Sort(a) sort(a.begin(), a.end())
#define rev(a) reverse(a.begin(), a.end())
#define fi first
#define se second
#define co(a) cout << a << endl
#define sz size()
#define bgn begin()
#define en end()
#define pb(a) push_back(a)
#define pp pop_back()
template <class T> void cou(vector<vector<T>> a) {
  int b = a.size();
  int c = a[0].size();
  fo(i, b) {
    fo(j, c) {
      cout << a[i][j];
      if (j == c - 1)
        cout << endl;
      else
        cout << ' ';
    }
  }
}
// template<>
void cou(vector<vector<char>> a) {
  int b = a.size();
  int c = a[0].size();
  fo(i, b) {
    fo(j, c) {
      cout << a[i][j];
      if (j == c - 1)
        cout << endl;
      else
        cout << ' ';
    }
  }
}
int wari(int a, int b) {
  if (a % b == 0)
    return a / b;
  else
    return a / b + 1;
}
int keta(int a) {
  double b = a;
  b = log10(b);
  int c = b;
  return c + 1;
}
int souwa(int a) { return a * (a + 1) / 2; }
int lcm(int a, int b) {
  int d = a, e = b, f;
  if (a < b)
    swap(a, b);
  int c, m = 1;
  while (m) {
    c = a % b;
    if (c == 0) {
      f = b;
      m--;
    } else {
      a = b;
      b = c;
    }
  }
  return d * e / f;
}
int gcm(int a, int b) {
  int d = a, e = b, f;
  if (a < b)
    swap(a, b);
  int c, m = 1;
  while (m) {
    c = a % b;
    if (c == 0) {
      f = b;
      m--;
    } else {
      a = b;
      b = c;
    }
  }
  return f;
}
bool prime(int a) {
  if (a < 2)
    return false;
  else if (a == 2)
    return true;
  else if (a % 2 == 0)
    return false;
  double b = sqrt(a);
  for (int i = 3; i <= b; i += 2) {
    if (a % i == 0) {
      return false;
    }
  }
  return true;
}
struct Union {
  vector<int> par;
  Union(int n) { par = vector<int>(n, -1); }
  int find(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = find(par[x]);
  }
  bool same(int a, int b) { return find(a) == find(b); }
  int Size(int a) { return -par[find(a)]; }
  void unite(int a, int b) {
    a = find(a);
    b = find(b);
    if (a == b)
      return;
    if (Size(b) > Size(a))
      swap<int>(a, b);
    par[a] += par[b];
    par[b] = a;
  }
};
signed main() {
  int a, b, e, f = 1000000000;
  cin >> a >> b;
  vector<int> c, d;
  fo(i, a) {
    cin >> e;
    if (e < 0)
      c.pb(-e);
    else
      d.pb(e);
  }
  Sort(c);
  for (int i = max<int>(0, b - c.sz - 1); i < min<int>(b, d.sz); i++) {
    f = min<int>(min<int>(f, d[i] * 2 + c[b - i - 2]), d[i] + c[b - i - 2] * 2);
  }
  if (c.sz >= b)
    f = min(f, c[b - 1]);
  if (d.sz >= b)
    f = min(f, d[b - 1]);
  if (c.sz == 0)
    f = d[b - 1];
  if (d.sz == 0)
    f = c[b - 1];
  cout << f << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fo(a, b) for (int a = 0; a < b; a++)
#define Sort(a) sort(a.begin(), a.end())
#define rev(a) reverse(a.begin(), a.end())
#define fi first
#define se second
#define co(a) cout << a << endl
#define sz size()
#define bgn begin()
#define en end()
#define pb(a) push_back(a)
#define pp pop_back()
template <class T> void cou(vector<vector<T>> a) {
  int b = a.size();
  int c = a[0].size();
  fo(i, b) {
    fo(j, c) {
      cout << a[i][j];
      if (j == c - 1)
        cout << endl;
      else
        cout << ' ';
    }
  }
}
// template<>
void cou(vector<vector<char>> a) {
  int b = a.size();
  int c = a[0].size();
  fo(i, b) {
    fo(j, c) {
      cout << a[i][j];
      if (j == c - 1)
        cout << endl;
      else
        cout << ' ';
    }
  }
}
int wari(int a, int b) {
  if (a % b == 0)
    return a / b;
  else
    return a / b + 1;
}
int keta(int a) {
  double b = a;
  b = log10(b);
  int c = b;
  return c + 1;
}
int souwa(int a) { return a * (a + 1) / 2; }
int lcm(int a, int b) {
  int d = a, e = b, f;
  if (a < b)
    swap(a, b);
  int c, m = 1;
  while (m) {
    c = a % b;
    if (c == 0) {
      f = b;
      m--;
    } else {
      a = b;
      b = c;
    }
  }
  return d * e / f;
}
int gcm(int a, int b) {
  int d = a, e = b, f;
  if (a < b)
    swap(a, b);
  int c, m = 1;
  while (m) {
    c = a % b;
    if (c == 0) {
      f = b;
      m--;
    } else {
      a = b;
      b = c;
    }
  }
  return f;
}
bool prime(int a) {
  if (a < 2)
    return false;
  else if (a == 2)
    return true;
  else if (a % 2 == 0)
    return false;
  double b = sqrt(a);
  for (int i = 3; i <= b; i += 2) {
    if (a % i == 0) {
      return false;
    }
  }
  return true;
}
struct Union {
  vector<int> par;
  Union(int n) { par = vector<int>(n, -1); }
  int find(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = find(par[x]);
  }
  bool same(int a, int b) { return find(a) == find(b); }
  int Size(int a) { return -par[find(a)]; }
  void unite(int a, int b) {
    a = find(a);
    b = find(b);
    if (a == b)
      return;
    if (Size(b) > Size(a))
      swap<int>(a, b);
    par[a] += par[b];
    par[b] = a;
  }
};
signed main() {
  int a, b, e, f = 1000000000;
  cin >> a >> b;
  vector<int> c, d;
  fo(i, a) {
    cin >> e;
    if (e < 0)
      c.pb(-e);
    else
      d.pb(e);
  }
  Sort(c);
  if (c.sz != 0 && d.sz != 0) {
    for (int i = max<int>(0, b - c.sz - 1); i < min<int>(b, d.sz); i++) {
      f = min<int>(min<int>(f, d[i] * 2 + c[b - i - 2]),
                   d[i] + c[b - i - 2] * 2);
    }
  }
  if (c.sz >= b)
    f = min(f, c[b - 1]);
  if (d.sz >= b)
    f = min(f, d[b - 1]);
  if (c.sz == 0)
    f = d[b - 1];
  if (d.sz == 0)
    f = c[b - 1];
  cout << f << endl;
}