prithivMLmods/Coder-Stat huggingface dataset:
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define base 1000000007LL
#define ll long long
#define X first
#define Y second
#define ld double
#define pb push_back
#define EPS 1e-9
#define all(a) a.begin(), a.end()
#define sz(a) int((a).size())
#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORE(i, a, b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a)                                                          \
  for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
using namespace std;
typedef pair<int, int> II;
typedef pair<ll, II> III;

int n, k, a[100010];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  // freopen("inp.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  cin >> n >> k;
  FOR(i, 1, n) cin >> a[i];
  int vt = 0;
  FOR(i, 1, n)
  if (a[i] >= 0) {
    vt = i;
    break;
  }
  if (vt == 0) {
    cout << abs(a[n - k + 1]);
    return 0;
  }
  if (a[vt] == 0) {
    if (k == 1) {
      cout << 0;
      return 0;
    }
  }
  int res = 2000000000;
  FOR(i, vt, n) {
    int remain = k - (i - vt + 1);
    if (remain < 0)
      break;
    if (vt - remain) {
      int h = a[i];
      int g = a[vt - remain];
      res = min(res, min(abs(g) + h - g, h - g + h));
    }
  }
  if (vt - k > 0) {
    int h = 0;
    int g = a[vt - k];
    res = min(res, min(abs(g) + h - g, h - g + h));
  }
  cout << res;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define base 1000000007LL
#define ll long long
#define X first
#define Y second
#define ld double
#define pb push_back
#define EPS 1e-9
#define all(a) a.begin(), a.end()
#define sz(a) int((a).size())
#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORE(i, a, b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a)                                                          \
  for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
using namespace std;
typedef pair<int, int> II;
typedef pair<ll, II> III;

int n, k, a[100010];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  // freopen("inp.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  cin >> n >> k;
  FOR(i, 1, n) cin >> a[i];
  int vt = 0;
  FOR(i, 1, n)
  if (a[i] >= 0) {
    vt = i;
    break;
  }
  if (vt == 0) {
    cout << abs(a[n - k + 1]);
    return 0;
  }
  if (a[vt] == 0) {
    if (k == 1) {
      cout << 0;
      return 0;
    }
  }
  int res = 2000000000;
  FOR(i, vt, n) {
    int remain = k - (i - vt + 1);
    if (remain < 0)
      break;
    if (vt - remain > 0) {
      int h = a[i];
      int g = a[vt - remain];
      res = min(res, min(abs(g) + h - g, h - g + h));
    }
  }
  if (vt - k > 0) {
    int h = 0;
    int g = a[vt - k];
    res = min(res, min(abs(g) + h - g, h - g + h));
  }
  cout << res;
  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
using namespace std;
const int N = 100010;
const int INF = 1e9;
int n, m;
int a[N];
inline int min(int x, int y) { return x < y ? x : y; }
void readData() {
  scanf("%d%d", &n, &m);
  a[0] = -INF;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  a[n + 1] = INF;
}
void solve() {
  int p;
  for (p = 1; p <= n && a[p] < 0; p++)
    ; // p is the first one which a[p]>=0
  int ans = INF;
  for (int i = p, j = 1; j <= m && i <= n; i++, j++)
    if (m - j <= i - 1)
      ans = min(ans, a[i] + ((m - j) ? a[i] - a[p - (m - j)] : 0));
  for (int i = p - 1, j = 1; j <= m && i; i--, j++)
    if (m - j <= n - p + 1)
      ans = min(ans, (-a[i]) + ((m - j) ? (-a[i]) + a[p + (m - j) - 1] : 0));
  printf("%d\n", ans);
}
int main() {
  readData();
  solve();
  return 0;
}


Fixed submission:
#include <cstdio>
using namespace std;
const int N = 100010;
const int INF = 1e9;
int n, m;
int a[N];
inline int min(int x, int y) { return x < y ? x : y; }
void readData() {
  scanf("%d%d", &n, &m);
  a[0] = -INF;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  a[n + 1] = INF;
}
void solve() {
  int p;
  for (p = 1; p <= n && a[p] < 0; p++)
    ; // p is the first one which a[p]>=0
  int ans = INF;
  for (int i = p, j = 1; j <= m && i <= n; i++, j++)
    if (m - j <= p - 1)
      ans = min(ans, a[i] + ((m - j) ? a[i] - a[p - (m - j)] : 0));
  for (int i = p - 1, j = 1; j <= m && i; i--, j++)
    if (m - j <= n - p + 1)
      ans = min(ans, (-a[i]) + ((m - j) ? (-a[i]) + a[p + (m - j) - 1] : 0));
  printf("%d\n", ans);
}
int main() {
  readData();
  solve();
  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define INF 1000000000000000
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
#define IN(a, n)                                                               \
  rep(i, n) { cin >> a[i]; }
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265
#define F first
#define S second
#define int ll

int n, k;
vector<int> a, b;

signed main() {
  cin >> n >> k;

  rep(i, n) {
    int c;
    cin >> c;
    if (c < 0) {
      c *= -1;
      a.push_back(c);
    } else
      b.push_back(c);
  }

  sort(all(a));

  int ans = INF;
  rep(i, a.size()) {
    ans = min(ans, a[i] * 2 + b[k - i - 2]);
    ans = min(ans, a[i] + b[k - i - 2] * 2);
  }

  if (a.size() >= k) {
    ans = min(ans, a[k - 1]);
  }
  if (b.size() >= k) {
    ans = min(ans, b[k - 1]);
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define INF 1000000000000000
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
#define IN(a, n)                                                               \
  rep(i, n) { cin >> a[i]; }
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265
#define F first
#define S second
#define int ll

int n, k;
vector<int> a, b;

signed main() {
  cin >> n >> k;

  rep(i, n) {
    int c;
    cin >> c;
    if (c < 0) {
      c *= -1;
      a.push_back(c);
    } else
      b.push_back(c);
  }

  sort(all(a));

  int ans = INF;
  rep(i, a.size()) {
    if (k - i - 2 < 0)
      break;
    if (k - i - 2 >= b.size())
      continue;
    ans = min(ans, a[i] * 2 + b[k - i - 2]);
    ans = min(ans, a[i] + b[k - i - 2] * 2);
  }

  if (a.size() >= k) {
    ans = min(ans, a[k - 1]);
  }
  if (b.size() >= k) {
    ans = min(ans, b[k - 1]);
  }

  cout << ans << endl;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>
#include <string.h>
#define N 120000
using namespace std;
int a[N], b[N], c[N], tot, cnt, n, k, ans = 0x7fffffff;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    if (a[i] == 0)
      k--;
  for (int i = 1; i <= n; i++) {
    if (a[i] > 0)
      b[++tot] = a[i];
  }
  for (int i = n; i >= 1; i--) {
    if (a[i] < 0)
      c[++cnt] = -a[i];
  }
  for (int i = 1; i <= tot; i++)
    if (c[k - i] != 0)
      ans = min(ans, b[i] + c[k - i] + min(b[i], c[k - i]));
  if (b[k] != 0)
    ans = min(ans, b[k]);
  if (c[k] != 0)
    ans = min(ans, c[k]);
  if (k == 0)
    ans = 0;
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <iostream>
#include <stdio.h>
#include <string.h>
#define N 120000
using namespace std;
int a[N], b[N], c[N], tot, cnt, n, k, ans = 0x7fffffff;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    if (a[i] == 0)
      k--;
  for (int i = 1; i <= n; i++) {
    if (a[i] > 0)
      b[++tot] = a[i];
  }
  for (int i = n; i >= 1; i--) {
    if (a[i] < 0)
      c[++cnt] = -a[i];
  }
  for (int i = 1; i <= tot; i++)
    if (k - i > 0 && c[k - i] != 0)
      ans = min(ans, b[i] + c[k - i] + min(b[i], c[k - i]));
  if (b[k] != 0)
    ans = min(ans, b[k]);
  if (c[k] != 0)
    ans = min(ans, c[k]);
  if (k == 0)
    ans = 0;
  printf("%d\n", ans);
  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

typedef long long LL;

int main() {
  int N, K;
  cin >> N >> K;
  vector<LL> pos_c, neg_c;
  LL c;
  for (LL i = 0; i < N; i++) {
    cin >> c;
    if (c > 0)
      pos_c.push_back(c);
    else if (c < 0)
      neg_c.push_back(abs(c));
    else
      K--;
  }

  int s_pos_c = pos_c.size();
  int s_neg_c = neg_c.size();
  LL cost = 1e9;
  sort(neg_c.begin(), neg_c.end());
  if (s_pos_c > 0 and s_neg_c > 0) {

    for (LL i = max(0, K - s_neg_c - 1); i < s_pos_c; i++)
      cost = min(cost, pos_c[i] + 2 * neg_c[K - i - 2]);

    for (LL i = max(0, K - s_pos_c - 1); i < s_neg_c; i++)
      cost = min(cost, neg_c[i] + 2 * pos_c[K - i - 2]);

  } else if (s_pos_c > 0)
    cost = pos_c[K - 1];
  else
    cost = neg_c[K - 1];

  cout << cost;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

typedef long long LL;

int main() {
  int N, K;
  cin >> N >> K;
  vector<LL> pos_c, neg_c;
  LL c;
  for (LL i = 0; i < N; i++) {
    cin >> c;
    if (c > 0)
      pos_c.push_back(c);
    else if (c < 0)
      neg_c.push_back(abs(c));
    else
      K--;
  }

  int s_pos_c = pos_c.size();
  int s_neg_c = neg_c.size();
  LL cost = 1e9;
  sort(neg_c.begin(), neg_c.end());
  if (K == 0)
    cost = 0;
  else if (K == 1)
    if (s_pos_c == 0)
      cost = neg_c[0];
    else
      cost = pos_c[0];
  else if (s_pos_c > 0 and s_neg_c > 0) {

    for (LL i = max(0, K - s_neg_c - 1); i < s_pos_c; i++)
      cost = min(cost, pos_c[i] + 2 * neg_c[K - i - 2]);

    for (LL i = max(0, K - s_pos_c - 1); i < s_neg_c; i++)
      cost = min(cost, neg_c[i] + 2 * pos_c[K - i - 2]);

  } else if (s_pos_c > 0)
    cost = pos_c[K - 1];
  else
    cost = neg_c[K - 1];

  cout << cost;

  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define maxn 1000000
int n, K, a[maxn], sum[maxn], sum2[maxn];
int ans = 999999999;

int read() {
  int x = 0, k = 1;
  char c;
  c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      k = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * k;
}

int main() {
  n = read(), K = read();
  int rec = 0;
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 1; i <= n; i++) {
    if (a[i] >= 0)
      break;
    rec = i;
  }
  for (int i = rec; i; i--)
    sum[rec - i + 1] = -a[i];
  for (int i = rec + 1; i <= n; i++)
    sum2[i - rec] = a[i];
  for (int i = 1; i <= rec; i++) {
    if (K - i >= 1)
      ;
    int tem = sum[i], tem2 = sum2[K - i];
    int tem3 = min(tem, tem2);
    ans = min(ans, tem + tem2 + tem3);
  }
  if (rec >= K)
    ans = min(ans, sum[K]);
  if (n - rec >= K)
    ans = min(ans, sum2[K]);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define maxn 1000000
int n, K, a[maxn], sum[maxn], sum2[maxn];
int ans = 999999999;

int read() {
  int x = 0, k = 1;
  char c;
  c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      k = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * k;
}

int main() {
  n = read(), K = read();
  int rec = 0;
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 1; i <= n; i++) {
    if (a[i] >= 0)
      break;
    rec = i;
  }
  for (int i = rec; i; i--)
    sum[rec - i + 1] = -a[i];
  for (int i = rec + 1; i <= n; i++)
    sum2[i - rec] = a[i];
  for (int i = 1; i <= rec; i++) {
    if (K - i < 1 || K - i > n - rec)
      continue;
    int tem = sum[i], tem2 = sum2[K - i];
    int tem3 = min(tem, tem2);
    ans = min(ans, tem + tem2 + tem3);
  }
  if (rec >= K)
    ans = min(ans, sum[K]);
  if (n - rec >= K)
    ans = min(ans, sum2[K]);
  cout << ans << endl;
  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define sz(x) (int)x.size()
#define unique(x) x.erase(unique(x.begin(), x.end()), x.end())
#define all(a) a.begin(), a.end()
#define sqr(x) ((x) * (x))
#define y1 aksjdaskdjksjfksdjf
#define left kdnvldvoiwejifejg
#define right lkdsjflksdjfdjfk
#define prev asdasfsadjkjsdfjs
#define tm aklsjdasjdasjdkdjjf
#define lcm hddalkwjeidjjhasda
#define random_shuffle asdasdd

int n, need;
vector<int> l, r;
int ans = 1e9;

int main() {

  scanf("%d %d", &n, &need);
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    if (x < 0) {
      l.push_back(-x);
    } else {
      r.push_back(x);
    }
  }

  sort(all(l));
  sort(all(r));

  if (sz(r) >= need) {
    ans = r[need - 1];
  }

  for (int i = 0; i < min(need, sz(l)); i++) {
    int rem = need - (i + 1);
    int curAns = l[i];
    if (rem) {
      curAns += l[i] + r[rem - 1];
    }
    if (rem) {
      curAns = min(curAns, 2 * r[rem - 1] + l[i]);
    }
    ans = min(ans, curAns);
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define sz(x) (int)x.size()
#define unique(x) x.erase(unique(x.begin(), x.end()), x.end())
#define all(a) a.begin(), a.end()
#define sqr(x) ((x) * (x))
#define y1 aksjdaskdjksjfksdjf
#define left kdnvldvoiwejifejg
#define right lkdsjflksdjfdjfk
#define prev asdasfsadjkjsdfjs
#define tm aklsjdasjdasjdkdjjf
#define lcm hddalkwjeidjjhasda
#define random_shuffle asdasdd

int n, need;
vector<int> l, r;
int ans = 1e9;

int main() {

  scanf("%d %d", &n, &need);
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    if (x < 0) {
      l.push_back(-x);
    } else {
      r.push_back(x);
    }
  }

  sort(all(l));
  sort(all(r));

  if (sz(r) >= need) {
    ans = r[need - 1];
  }

  for (int i = 0; i < min(need, sz(l)); i++) {
    int rem = need - (i + 1);
    if (rem > sz(r)) {
      continue;
    }
    int curAns = l[i];
    if (rem) {
      curAns += l[i] + r[rem - 1];
    }
    if (rem) {
      curAns = min(curAns, 2 * r[rem - 1] + l[i]);
    }
    ans = min(ans, curAns);
  }

  cout << ans << endl;

  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define IO_OP                                                                  \
  std::ios::sync_with_stdio(0);                                                \
  std::cin.tie(0);
#define F first
#define S second
#define PB push_back
#define MP make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;

const int INF = 1e9 + 7;

int a[10000];

int main() {
  IO_OP;

  int n, k;
  cin >> n >> k;
  vi l, r;
  l.PB(0), r.PB(0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 0)
      k--;
  }
  for (int i = 0; i < n; i++)
    if (a[i] > 0)
      r.PB(a[i]);
  for (int i = n - 1; i >= 0; i--)
    if (a[i] < 0)
      l.PB(a[i]);
  int ans = INF;
  for (int i = 0; i <= k; i++) {
    int j = k - i;
    if (i >= r.size())
      continue;
    if (j >= l.size())
      continue;
    ans = min(ans, min(r[i] * 2 - l[j], -l[j] * 2 + r[i]));
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define IO_OP                                                                  \
  std::ios::sync_with_stdio(0);                                                \
  std::cin.tie(0);
#define F first
#define S second
#define PB push_back
#define MP make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;

const int INF = 1e9 + 7;

int a[100005];

int main() {
  IO_OP;

  int n, k;
  cin >> n >> k;
  vi l, r;
  l.PB(0), r.PB(0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 0)
      k--;
  }
  for (int i = 0; i < n; i++)
    if (a[i] > 0)
      r.PB(a[i]);
  for (int i = n - 1; i >= 0; i--)
    if (a[i] < 0)
      l.PB(a[i]);
  int ans = INF;
  for (int i = 0; i <= k; i++) {
    int j = k - i;
    if (i >= r.size())
      continue;
    if (j >= l.size())
      continue;
    ans = min(ans, min(r[i] * 2 - l[j], -l[j] * 2 + r[i]));
  }
  cout << ans << endl;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int MOD = 1000000007;

int main() {
  int N, K;
  cin >> N >> K;
  ll x[10001];
  for (int i = 0; i < N; i++) {
    cin >> x[i];
  }
  sort(x, x + N);
  ll ans = INT64_MAX;
  for (int i = 0; i < N - K + 1; i++) {
    ll l = x[i], r = x[i + K - 1];
    ll s;
    if (l * r >= 0)
      s = max(abs(l), abs(r));
    else
      s = min(abs(l), abs(r)) + (r - l);
    ans = min(ans, s);
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int MOD = 1000000007;

int main() {
  int N, K;
  cin >> N >> K;
  ll x[100001];
  for (int i = 0; i < N; i++) {
    cin >> x[i];
  }
  sort(x, x + N);
  ll ans = INT64_MAX;
  for (int i = 0; i < N - K + 1; i++) {
    ll l = x[i], r = x[i + K - 1];
    ll s;
    if (l * r >= 0)
      s = max(abs(l), abs(r));
    else
      s = min(abs(l), abs(r)) + (r - l);
    ans = min(ans, s);
  }

  cout << ans << endl;

  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vl;

#define REP(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)

ll a, b, c;
ll N, M, K;

void solve() {
  cin >> N >> K;
  ll x[N];
  rep(i, N) cin >> x[i];
  ll ans = (ll)1e18;
  rep(i, K) {
    if ((x[i] < 0 && x[i + K - 1] > 0) || (x[i] > 0 && x[i + K - 1] < 0)) {
      ll tmp = (x[i + K - 1] - x[i]) + min(abs(x[i]), abs(x[i + K - 1]));
      ans = min(ans, tmp);
    } else {
      ans = min(ans, max(abs(x[i]), abs(x[i + K - 1])));
    }
  }
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vl;

#define REP(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)

ll a, b, c;
ll N, M, K;

void solve() {
  cin >> N >> K;
  ll x[N];
  rep(i, N) cin >> x[i];
  ll ans = (ll)1e18;
  rep(i, N - K + 1) {
    if ((x[i] < 0 && x[i + K - 1] > 0) || (x[i] > 0 && x[i + K - 1] < 0)) {
      ll tmp = (x[i + K - 1] - x[i]) + min(abs(x[i]), abs(x[i + K - 1]));
      ans = min(ans, tmp);
    } else {
      ans = min(ans, max(abs(x[i]), abs(x[i + K - 1])));
    }
  }
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <fstream>
#include <iostream>

using namespace std;

int main() {
  int n, k;
  int i, j;
  int intArray[100000];
  int intArrayCostLR[100000];
  int intArrayCostRL[100000];
  int minimumCost;

  cin >> n >> k;

  cin >> intArray[0];
  for (i = 1; i < n; i++) {
    cin >> intArray[i];
    intArrayCostLR[i] = 0;
    intArrayCostRL[i] = 0;
  }
  minimumCost = 0x7FFFFFFF;
  for (i = k - 1; i < n; i++) {
    intArrayCostLR[i] =
        abs(intArray[i - k + 1]) + abs(intArray[i - k + 1] - intArray[i]);
    if (minimumCost > intArrayCostLR[i]) {
      minimumCost = intArrayCostLR[i];
    }
  }
  for (i = 0; i < n - k + 1; i++) {
    intArrayCostRL[i] =
        abs(intArray[i + k - 1]) + abs(intArray[i + k - 1] - intArray[i]);
    if (minimumCost > intArrayCostRL[i]) {
      minimumCost = intArrayCostRL[i];
    }
  }

  cout << minimumCost;

  return 1;
}

Fixed submission:
#include <cmath>
#include <fstream>
#include <iostream>

using namespace std;

int main() {
  int n, k;
  int i, j;
  int intArray[100000];
  int intArrayCostLR[100000];
  int intArrayCostRL[100000];
  int minimumCost;

  cin >> n >> k;

  cin >> intArray[0];
  for (i = 1; i < n; i++) {
    cin >> intArray[i];
    intArrayCostLR[i] = 0;
    intArrayCostRL[i] = 0;
  }
  minimumCost = 0x7FFFFFFF;
  for (i = k - 1; i < n; i++) {
    intArrayCostLR[i] =
        abs(intArray[i - k + 1]) + abs(intArray[i - k + 1] - intArray[i]);
    if (minimumCost > intArrayCostLR[i]) {
      minimumCost = intArrayCostLR[i];
    }
  }
  for (i = 0; i < n - k + 1; i++) {
    intArrayCostRL[i] =
        abs(intArray[i + k - 1]) + abs(intArray[i + k - 1] - intArray[i]);
    if (minimumCost > intArrayCostRL[i]) {
      minimumCost = intArrayCostRL[i];
    }
  }

  cout << minimumCost;

  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<long, long> PLL;

// container util
//------------------------------------------
#define PB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// repetition
//------------------------------------------
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
// constant
//--------------------------------------------
// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

const double EPS = 1e-10;

int main() {
  int N, K;
  cin >> N >> K;
  vector<LL> x(N);
  REP(i, N) cin >> x[i];
  LL res = 1e10;
  REP(ld, N - K + 1) FOR(rd, ld + K - 1, N) {
    if (x[rd] <= 0) {
      res = min(res, abs(x[ld]));
    } else if (x[ld] >= 0) {
      res = min(res, abs(x[rd]));
    } else if (x[rd] > 0) {
      res = min(res, min(abs(x[ld]) * 2 + x[rd], abs(x[ld]) + x[rd] * 2));
    }
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<long, long> PLL;

// container util
//------------------------------------------
#define PB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// repetition
//------------------------------------------
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
// constant
//--------------------------------------------
// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

const double EPS = 1e-10;

int main() {
  int N, K;
  cin >> N >> K;
  vector<LL> x(N);
  REP(i, N) cin >> x[i];
  LL res = 1e10;
  REP(ld, N - K + 1) {
    LL rd = ld + K - 1;
    if (x[rd] <= 0) {
      res = min(res, abs(x[ld]));
    } else if (x[ld] >= 0) {
      res = min(res, abs(x[rd]));
    } else if (x[rd] > 0) {
      res = min(res, min(abs(x[ld]) * 2 + x[rd], abs(x[ld]) + x[rd] * 2));
    }
  }
  cout << res << endl;
  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

typedef long long int ll;
typedef unsigned long long int ull;
#define rep(i, n) for (auto i = 0; i < (n); i++)

using namespace std;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, k;
  cin >> n >> k;
  vector<ll> x(n);
  rep(i, n) cin >> x[i];

  int idl = 0;
  int idr = 0;
  rep(i, n) {
    if (x[i] <= 0)
      idl = i;
    if (0 <= x[i]) {
      idr = i;
      break;
    }
  }

  if (idl == idr) {
    k--;
    idl--;
    idr++;
  }

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

  vector<pair<int, ll>> left(idl + 1);
  rep(i, idl + 1) { left[i] = {idl - i + 1, (ll)abs(x[i])}; }

  vector<pair<int, ll>> rr(n - idr + 5);
  for (int i = idr; i < n; i++) {
    rr[i - idr] = {i - idr + 1, (ll)abs(x[i])};
  }

  ll minn = LLONG_MAX;
  rep(l, idl + 1) {
    if (left[l].first == k)
      minn = min(minn, left[l].second);
  }
  rep(r, n - idr) {
    if (rr[r].first == k)
      minn = min(minn, rr[r].second);
  }

  rep(l, idl + 1) {
    if (k < left[l].first)
      break;
    rep(r, n - idr) {
      if (k < left[l].first + rr[r].first)
        break;
      if (left[l].first + rr[r].first < k)
        continue;

      ll cost = left[l].second + rr[r].second;
      cost += min(left[l].second, rr[r].second);
      minn = min(minn, cost);
    }
  }

  cout << minn << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

typedef long long int ll;
typedef unsigned long long int ull;
#define rep(i, n) for (auto i = 0; i < (n); i++)

using namespace std;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, k;
  cin >> n >> k;
  vector<ll> x(n);
  rep(i, n) cin >> x[i];

  ll minn = 1e10;
  for (int l = 0; l + k <= n; l++) {
    int r = l + k - 1;
    if (x[r] <= 0) {
      minn = min(minn, abs(x[l]));
    } else if (0 <= x[l]) {
      minn = min(minn, abs(x[r]));
    } else {
      minn = min(minn, abs(x[l]) + abs(x[r]) + min(abs(x[l]), abs(x[r])));
    }
  }

  cout << minn << endl;

  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<long long> l, r, s;
long long n, k, ans = 1000000000;

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a == 0) {
      k--;
    } else if (a > 0) {
      r.push_back(a);
    } else {
      s.push_back(a);
    }
  }
  for (int i = s.size() - 1; i >= 0; i--) {
    l.push_back(s[i] * -1);
  }
  for (int i = 0; i <= k; i++) {
    if (l.size() >= i && r.size() >= k - i) {
      if (i == 0) {
        ans = min(r[k - 1], ans);
      } else if (i == k) {
        ans = min(l[i - 1], ans);
      } else {
        ans = min(ans, l[i - 1] + r[k - i - 1] + min(l[i - 1], r[k - i - 1]));
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<long long> l, r, s;
long long n, k, ans = 1000000000;

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a == 0) {
      k--;
      if (k == 0) {
        cout << 0 << endl;
        return 0;
      }
    } else if (a > 0) {
      r.push_back(a);
    } else {
      s.push_back(a);
    }
  }
  for (int i = s.size() - 1; i >= 0; i--) {
    l.push_back(s[i] * -1);
  }
  for (int i = 0; i <= k; i++) {
    if (l.size() >= i && r.size() >= k - i) {
      if (i == 0) {
        ans = min(r[k - 1], ans);
      } else if (i == k) {
        ans = min(l[i - 1], ans);
      } else {
        ans = min(ans, l[i - 1] + r[k - i - 1] + min(l[i - 1], r[k - i - 1]));
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, k, t, a[N];
void Get_t() {
  t = -1;
  for (int i = 1; i <= n; i++)
    if (a[i] == 0)
      t = i;
  if (~t)
    return;
  k++;
  a[++n] = 0;
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    if (a[i] == 0)
      t = i;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int ans = 1e9;
  Get_t();
  //	printf("%d\n",t);
  //	for (int i=1;i<=n;i++)
  //		printf("%d ",a[i]);puts("");
  if (n - t + 1 >= k)
    ans = min(ans, abs(a[t + k - 1]));
  if (t >= k)
    ans = min(ans, abs(a[t - k + 1]));
  for (int i = max(1, k - t); i <= min(n - t, k - 1); i++)
    ans = min(ans, abs(a[t - (k - i) + 1]) + 2 * abs(a[t + i]));
  for (int i = max(1, k - t); i <= min(t - 1, k - 1); i++)
    ans = min(ans, abs(a[t + (k - i) - 1]) + 2 * abs(a[t - i]));
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, k, t, a[N];
void Get_t() {
  t = -1;
  for (int i = 1; i <= n; i++)
    if (a[i] == 0)
      t = i;
  if (~t)
    return;
  k++;
  a[++n] = 0;
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    if (a[i] == 0)
      t = i;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int ans = 1e9;
  Get_t();
  //	printf("%d\n",t);
  //	for (int i=1;i<=n;i++)
  //		printf("%d ",a[i]);puts("");
  if (n - t + 1 >= k)
    ans = min(ans, abs(a[t + k - 1]));
  if (t >= k)
    ans = min(ans, abs(a[t - k + 1]));
  for (int i = max(1, k - t); i <= min(n - t, k - 1); i++)
    ans = min(ans, abs(a[t - (k - i) + 1]) + 2 * abs(a[t + i]));
  for (int i = max(1, k - (n - t + 1)); i <= min(t - 1, k - 1); i++)
    ans = min(ans, abs(a[t + (k - i) - 1]) + 2 * abs(a[t - i]));
  printf("%d", ans);
  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<vector<int>> x(2, vector<int>(1, 0));
  vector<int> cnt(2, 0);
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    if (tmp < 0) {
      x[0].insert(x[0].begin() + 1, -tmp);
      cnt[0]++;
    } else {
      x[1].push_back(tmp);
      cnt[1]++;
    }
  }
  int sgl = 0;
  int dbl = 1;
  int ret = 2e9;
  for (int times = 0; times < 2; times++) {
    for (int i = max(0, k - cnt[dbl]); i <= cnt[sgl]; i++) {
      ret = min(ret, x[sgl][i] + 2 * x[dbl][k - i]);
    }
    swap(sgl, dbl);
  }
  cout << ret << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<vector<int>> x(2, vector<int>(1, 0));
  vector<int> cnt(2, 0);
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    if (tmp < 0) {
      x[0].insert(x[0].begin() + 1, -tmp);
      cnt[0]++;
    } else {
      x[1].push_back(tmp);
      cnt[1]++;
    }
  }
  int sgl = 0;
  int dbl = 1;
  int ret = 2e9;
  for (int times = 0; times < 2; times++) {
    for (int i = max(0, k - cnt[dbl]); i <= min(cnt[sgl], k); i++) {
      ret = min(ret, x[sgl][i] + 2 * x[dbl][k - i]);
    }
    swap(sgl, dbl);
  }
  cout << ret << '\n';
  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

#define REP(i, n, N) for (ll i = (n); i < (N); i++)
#define RREP(i, n, N) for (ll i = (N - 1); i >= n; i--)
#define CK(n, a, b) ((a) <= (n) && (n) < (b))
#define ALL(v) (v).begin(), (v).end()
#define MCP(a, b) memcpy(b, a, sizeof(b))
#define p(s) cout << (s) << endl
#define p2(a, b) cout << (a) << " " << (b) << endl
#define v2(T) vector<vector<T>>
typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
const ll inf = 1e18;

ll N, K;
ll ans;
vector<ll> mi, pl;

int main() {
  cin >> N >> K;
  ll a;
  REP(i, 0, N) {
    cin >> a;
    if (a < 0)
      mi.push_back(-a);
    else
      pl.push_back(a);
  }
  ans = inf;
  reverse(ALL(mi));
  if (mi.size() >= K)
    ans = min(ans, mi[K - 1]);
  if (pl.size() >= K)
    ans = min(ans, pl[K - 1]);
  REP(i, 0, mi.size()) {
    if (K - i - 2 >= 0 && K - i - 2 <= pl.size()) {
      ans = min(ans, mi[i] + pl[K - i - 2] + min<ll>(mi[i], pl[K - i - 2]));
    }
  }
  p(ans);
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"

#define REP(i, n, N) for (ll i = (n); i < (N); i++)
#define RREP(i, n, N) for (ll i = (N - 1); i >= n; i--)
#define CK(n, a, b) ((a) <= (n) && (n) < (b))
#define ALL(v) (v).begin(), (v).end()
#define MCP(a, b) memcpy(b, a, sizeof(b))
#define p(s) cout << (s) << endl
#define p2(a, b) cout << (a) << " " << (b) << endl
#define v2(T) vector<vector<T>>
typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
const ll inf = 1e18;

ll N, K;
ll ans;
vector<ll> mi, pl;

int main() {
  cin >> N >> K;
  ll a;
  REP(i, 0, N) {
    cin >> a;
    if (a < 0)
      mi.push_back(-a);
    else
      pl.push_back(a);
  }
  ans = inf;
  reverse(ALL(mi));
  if (mi.size() >= K)
    ans = min(ans, mi[K - 1]);
  if (pl.size() >= K)
    ans = min(ans, pl[K - 1]);
  REP(i, 0, mi.size()) {
    if (K - i - 2 >= 0 && K - i - 2 < pl.size()) {
      ans = min(ans, mi[i] + pl[K - i - 2] + min<ll>(mi[i], pl[K - i - 2]));
    }
  }
  p(ans);
  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using std::pair;
using std::string;
using std::vector;

typedef long long ll;
typedef pair<int, int> pii;

#define fst first
#define snd second
#define pb(a) push_back(a)
#define mp(a, b) std::make_pair(a, b)
#define debug(...) fprintf(stderr, __VA_ARGS__)

template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }

const int oo = 0x3f3f3f3f;

string procStatus() {
  std::ifstream t("/proc/self/status");
  return string(std::istreambuf_iterator<char>(t),
                std::istreambuf_iterator<char>());
}

template <typename T> T read(T &x) {
  int f = 1;
  x = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - 48;
  return x *= f;
}

const int N = 100000;

int n, k;
int a[N + 5];

int main() {
#ifdef Wearry
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif

  read(n), read(k);
  for (int i = 1; i <= n; ++i)
    read(a[i]);

  if (a[1] >= 0)
    return printf("%d\n", a[k]), 0;
  if (a[n] <= 0)
    return printf("%d\n", -a[n - k + 1]), 0;

  int ans = INT_MAX;
  for (int i = 1; i <= n; ++i)
    if (a[i] <= 0 && a[i + k - 1] >= 0) {
      chkmin(ans, -a[i] + a[i + k - 1] - a[i]);
      chkmin(ans, a[i + k - 1] * 2 - a[i]);
    }
  printf("%d\n", ans);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using std::pair;
using std::string;
using std::vector;

typedef long long ll;
typedef pair<int, int> pii;

#define fst first
#define snd second
#define pb(a) push_back(a)
#define mp(a, b) std::make_pair(a, b)
#define debug(...) fprintf(stderr, __VA_ARGS__)

template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }

const int oo = 0x3f3f3f3f;

string procStatus() {
  std::ifstream t("/proc/self/status");
  return string(std::istreambuf_iterator<char>(t),
                std::istreambuf_iterator<char>());
}

template <typename T> T read(T &x) {
  int f = 1;
  x = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - 48;
  return x *= f;
}

const int N = 100000;

int n, k;
int a[N + 5];

int main() {
#ifdef Wearry
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif

  read(n), read(k);
  for (int i = 1; i <= n; ++i)
    read(a[i]);

  if (a[1] >= 0)
    return printf("%d\n", a[k]), 0;
  if (a[n] <= 0)
    return printf("%d\n", -a[n - k + 1]), 0;

  int ans = INT_MAX;
  for (int i = 1; i + k - 1 <= n; ++i)
    if (a[i] <= 0 && a[i + k - 1] >= 0) {
      chkmin(ans, -a[i] + a[i + k - 1] - a[i]);
      chkmin(ans, a[i + k - 1] * 2 - a[i]);
    }
  printf("%d\n", ans);

  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define ll long long

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> neg;
  vector<int> noneg;
  int temp;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    if (temp < 0)
      neg.push_back(temp);
    else
      noneg.push_back(temp);
  }
  int t = neg.size();
  int l = n - t;
  reverse(neg.begin(), neg.end());
  int minn = 1000000000;
  if (t >= k)
    minn = min(minn, -neg[k - 1]);
  if (l >= k)
    minn = min(minn, noneg[k - 1]);
  for (int i = 1; i <= min(k, t); i++) {
    int j = k - i;
    if (l >= j) {
      minn = min(minn, abs(2 * neg[i - 1]) + noneg[j - 1]);
      minn = min(minn, 2 * noneg[j - 1] + abs(neg[i - 1]));
    }
  }
  cout << minn;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define ll long long

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> neg;
  vector<int> noneg;
  int temp;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    if (temp < 0)
      neg.push_back(temp);
    else
      noneg.push_back(temp);
  }
  int t = neg.size();
  int l = n - t;
  reverse(neg.begin(), neg.end());
  int minn = 1000000000;
  if (t >= k)
    minn = min(minn, -neg[k - 1]);
  if (l >= k)
    minn = min(minn, noneg[k - 1]);
  for (int i = 1; i <= min(k, t); i++) {
    int j = k - i;
    if (l >= j && j > 0) {
      minn = min(minn, abs(2 * neg[i - 1]) + noneg[j - 1]);
      minn = min(minn, 2 * noneg[j - 1] + abs(neg[i - 1]));
    }
  }
  cout << minn;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;

int N, K;
vector<int> RL[2];
int ans = 1e9;

int main() {
  cin >> N >> K;
  int x;
  rep(i, N) {
    scanf("%d", &x);
    RL[(int)(x >= 0)].push_back(abs(x));
  }
  sort(RL[0].begin(), RL[0].end());
  sort(RL[1].begin(), RL[1].end());
  for (int i = min(K, (int)RL[0].size()) - 1;
       i >= max(0, (int)(K - RL[1].size() - 1)); i--) {
    ans = min(ans, RL[0][i] * 2 + RL[1][K - i - 2]);
    ans = min(ans, RL[0][i] + RL[1][K - i - 2] * 2);
  }
  if (RL[0].size() >= K) {
    ans = min(ans, RL[0][K - 1]);
  }
  if (RL[1].size() >= K) {
    ans = min(ans, RL[1][K - 1]);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;

int N, K;
vector<int> RL[2];
int ans = 1e9;

int main() {
  cin >> N >> K;
  int x;
  rep(i, N) {
    scanf("%d", &x);
    RL[(int)(x >= 0)].push_back(abs(x));
  }
  sort(RL[0].begin(), RL[0].end());
  sort(RL[1].begin(), RL[1].end());
  for (int i = min(K - 1, (int)RL[0].size()) - 1;
       i >= max(0, (int)(K - RL[1].size() - 1)); i--) {
    ans = min(ans, RL[0][i] * 2 + RL[1][K - i - 2]);
    ans = min(ans, RL[0][i] + RL[1][K - i - 2] * 2);
  }
  if (RL[0].size() >= K) {
    ans = min(ans, RL[0][K - 1]);
  }
  if (RL[1].size() >= K) {
    ans = min(ans, RL[1][K - 1]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
const int N = 1005;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
typedef long long ll;

int n, k;
int x[N];
int Abs(int x) { return x < 0 ? -x : x; }
int main() {
  while (~scanf("%d %d", &n, &k)) {
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &x[i]);
    }

    int ans = INF;
    for (int i = 1; i <= n - k + 1; ++i) {
      if (x[i] <= 0 && x[i + k - 1] >= 0) {
        ans = min(ans, min(-x[i], x[i + k - 1]) + x[i + k - 1] - x[i]);
      } else {
        ans = min(ans, max(Abs(x[i]), Abs(x[i + k - 1])));
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
typedef long long ll;

int n, k;
int x[N];
int Abs(int x) { return x < 0 ? -x : x; }
int main() {
  while (~scanf("%d %d", &n, &k)) {
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &x[i]);
    }

    int ans = INF;
    for (int i = 1; i <= n - k + 1; ++i) {
      if (x[i] <= 0 && x[i + k - 1] >= 0) {
        ans = min(ans, min(-x[i], x[i + k - 1]) + x[i + k - 1] - x[i]);
      } else {
        ans = min(ans, max(Abs(x[i]), Abs(x[i + k - 1])));
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <unordered_map>
#include <numeric>

using namespace std;

const int mod = 1e9 + 7;

int n, k, a[123456], b[123456];
set<pair<int, int>> q;
long long cres = 1e18;

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (q.size() == k) {
      q.erase((make_pair(a[i - k], i - k)));
    }
    q.insert(make_pair(a[i], i));
    if (q.size() != k)
      continue;
    b[1] = (*q.begin()).first;
    int len = 2;
    b[len] = (*q.rbegin()).first;
    long long res = -1e18;
    if (b[1] <= 0 && b[len] >= 0) {
      res = min(-b[1] * 2 + b[len], b[len] * 2 - b[1]);
    }
    if (b[1] <= 0 && b[len] <= 0) {
      res = b[1];
    }
    if (b[1] >= 0 && b[len] >= 0) {
      res = b[len];
    }
    if (res < 0) {
      assert(0);
    }
    cres = min(cres, res);
  }
  cout << cres;
  return 0;
}


Fixed submission:
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <unordered_map>
#include <numeric>

using namespace std;

const int mod = 1e9 + 7;

int n, k, a[123456], b[123456];
set<pair<int, int>> q;
long long cres = 1e18;

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (q.size() == k) {
      q.erase((make_pair(a[i - k], i - k)));
    }
    q.insert(make_pair(a[i], i));
    if (q.size() != k)
      continue;
    b[1] = (*q.begin()).first;
    int len = 2;
    b[len] = (*q.rbegin()).first;
    long long res = -1e18;
    if (b[1] <= 0 && b[len] >= 0) {
      res = min(-b[1] * 2 + b[len], b[len] * 2 - b[1]);
    }
    if (b[1] <= 0 && b[len] <= 0) {
      res = -b[1];
    }
    if (b[1] >= 0 && b[len] >= 0) {
      res = b[len];
    }
    if (res < 0) {
      assert(0);
    }
    cres = min(cres, res);
  }
  cout << cres;
  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> xp;
  vector<ll> xm;
  rep(i, n) {
    ll x;
    cin >> x;
    if (x >= 0) {
      xp.push_back(x);
    } else {
      xm.push_back(-x);
    }
  }
  sort(xm.begin(), xm.end());
  int pn = xp.size();
  int mn = xm.size();
  ll ans = 1e12;
  if (pn < k) {
    ans = min(ans, xm[k - 1]);
  }
  rep(i, pn) {
    if (i >= k - 1) {
      ans = min(ans, xp[i]);
    } else {
      int j = k - (i + 1);
      if (j <= mn) {
        ans = min(ans, min(2 * xp[i] + xm[j - 1], xp[i] + 2 * xm[j - 1]));
        ;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> xp;
  vector<ll> xm;
  rep(i, n) {
    ll x;
    cin >> x;
    if (x >= 0) {
      xp.push_back(x);
    } else {
      xm.push_back(-x);
    }
  }
  sort(xm.begin(), xm.end());
  int pn = xp.size();
  int mn = xm.size();
  ll ans = 1e12;
  if (mn >= k) {
    ans = min(ans, xm[k - 1]);
  }
  rep(i, pn) {
    if (i >= k - 1) {
      ans = min(ans, xp[i]);
    } else {
      int j = k - (i + 1);
      if (j <= mn) {
        ans = min(ans, min(2 * xp[i] + xm[j - 1], xp[i] + 2 * xm[j - 1]));
        ;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03277 (C++)

Buggy submission (status: Runtime Error):
// Solution：二分答案。
#include <bits/stdc++.h>
inline int read() {
  int s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
using namespace std;
int n = read(), a[1000003];
int tree[2000003];
int find(int x) {
  int res = 0;
  while (x)
    res += tree[x], x -= x & (-x);
  return res;
}
int add(int x) {
  while (x <= 2000000)
    ++tree[x], x += x & (-x);
}
bool check(int x) {
  long long sum = 0;
  int k = 1000000;
  memset(tree, 0, sizeof(tree)), add(1000000);
  for (int i = 1; i <= n; i++) {
    if (a[i] >= x)
      ++k;
    else
      --k;
    sum += find(k), add(k);
  }
  return sum >= 1ll * n * (n + 1) / 4;
}
int main() {
  for (int i = 1; i <= n; i++)
    a[i] = read();
  if (n == 1)
    printf("%d\n", a[1]), exit(0);
  int l = 0, r = 1000000001, ans = 0;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid))
      ans = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
// Solution：二分答案。
#include <bits/stdc++.h>
inline int read() {
  int s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
using namespace std;
int n = read(), a[1000003];
int tree[2000003];
int find(int x) {
  int res = 0;
  while (x)
    res += tree[x], x -= x & (-x);
  return res;
}
void add(int x) {
  while (x <= 2000000)
    ++tree[x], x += x & (-x);
}
bool check(int x) {
  long long sum = 0;
  int k = 1000000;
  memset(tree, 0, sizeof(tree)), add(1000000);
  for (int i = 1; i <= n; i++) {
    if (a[i] >= x)
      ++k;
    else
      --k;
    sum += find(k), add(k);
  }
  return sum >= 1ll * n * (n + 1) / 4;
}
int main() {
  for (int i = 1; i <= n; i++)
    a[i] = read();
  if (n == 1)
    printf("%d\n", a[1]), exit(0);
  int l = 0, r = 1000000001, ans = 0;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid))
      ans = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  printf("%d\n", ans);
  return 0;
}