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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
long long x[100000005];
long long ans = 400000000;
int main(void) {
  int n, k;
  cin >> n >> k;

  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  for (int i = 0; i < n - k + 1; i++) {
    for (int j = i; j < i + k; j++) {
      if (x[i] * x[i + k - 1] < 0)
        ans = min(ans, min(abs(x[i]), abs(x[i + k - 1])) * 2 +
                           max(abs(x[i]), abs(x[i + k - 1])));
      else
        ans = min(ans, max(abs(x[i]), abs(x[i + k - 1])));
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
long long x[100000005];
long long ans = 400000000;
int main(void) {
  int n, k;
  cin >> n >> k;

  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  for (int i = 0; i < n - k + 1; i++) {

    if (x[i] * x[i + k - 1] < 0)
      ans = min(ans, min(abs(x[i]), abs(x[i + k - 1])) * 2 +
                         max(abs(x[i]), abs(x[i + k - 1])));
    else
      ans = min(ans, max(abs(x[i]), abs(x[i + k - 1])));
  }
  cout << ans << endl;

  return 0;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
typedef long long ll;

#define REP(i, N) for (int i = 0; i < (N); i++)
#define REPP(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define VSORT(c) sort((c).begin(), (c).end())
#define SZ(x) ((int)(x).size())

using namespace std;

int N, K;
int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> K;
  vector<int> array;
  vector<int> len;
  int num;
  int temp = -1, lenm, lenp;
  REP(i, N) {
    cin >> num;
    if (i != 0) {
      if (array[i - 1] < 0 && num >= 0) {
        temp = i;
      }
    } else {
      if (num >= 0) {
        temp = i;
      }
    }
    array.push_back(num);
  }
  if (temp == -1)
    temp = array.size();
  for (int i = temp - K; i != temp; i++) {
    if (i >= 0 && i + K - 1 < array.size()) {
      lenm = array[i];
      lenp = array[i + K - 1];
      len.push_back(2 * abs(lenm) + abs(lenp));
      len.push_back(abs(lenm) + 2 * abs(lenp));
      // cout << lenm << "   " << lenp << endl;
    }
  }
  if (N != K) {
    if (temp >= K)
      len.push_back(abs(array[temp - K]));
    if (array.size() >= temp + K - 1)
      len.push_back(abs(array[temp + K - 1]));
  }
  // REP(i,len.size()) cout << len[i] << endl;
  VSORT(len);
  cout << len[0] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
typedef long long ll;

#define REP(i, N) for (int i = 0; i < (N); i++)
#define REPP(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define VSORT(c) sort((c).begin(), (c).end())
#define SZ(x) ((int)(x).size())

using namespace std;

int N, K;
int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> K;
  vector<int> array;
  vector<int> len;
  int num;
  int temp = -1, lenm, lenp;
  REP(i, N) {
    cin >> num;
    if (i != 0) {
      if (array[i - 1] < 0 && num >= 0) {
        temp = i;
      }
    } else {
      if (num >= 0) {
        temp = i;
      }
    }
    array.push_back(num);
  }
  if (temp == -1)
    temp = array.size();
  for (int i = temp - K; i != temp; i++) {
    if (i >= 0 && i + K - 1 < array.size()) {
      lenm = array[i];
      lenp = array[i + K - 1];
      len.push_back(2 * abs(lenm) + abs(lenp));
      len.push_back(abs(lenm) + 2 * abs(lenp));
      // cout << lenm << "   " << lenp << endl;
    }
  }
  if (temp >= K) {
    len.push_back(abs(array[temp - K]));
  }
  if (array.size() > temp + K - 1) {
    len.push_back(abs(array[temp + K - 1]));
  }
  // REP(i,len.size()) cout << len[i] << endl;
  VSORT(len);
  cout << len[0] << endl;

  return 0;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define sc(x) scanf("%d", &x);
int main() {
  int n, k;
  sc(n) sc(k) vector<int> x(n), neg, pos;
  for (int i = 0; i < n; i++)
    sc(x[i]) int zero = 0, now = 0, ans = 1e9, tmp;
  for (int i = 0; i < n; i++) {
    if (x[i] > 0)
      break;
    else
      zero++;
  }
  for (int i = max(0, zero - k + 1); i < min(n, zero + k + 1); i++) {
    tmp = abs(x[i]);
    for (int j = i; j < i + k - 1; j++)
      tmp += abs(x[j + 1] - x[j]);
    ans = min(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define sc(x) scanf("%d", &x);
int main() {
  int n, k;
  sc(n) sc(k) vector<int> x(n), neg, pos;
  for (int i = 0; i < n; i++)
    sc(x[i]) int zero = 0, now = 0, ans = 1e9, tmp;
  for (int i = 0; i < n - k + 1; i++) {
    tmp = min(abs(x[i]), abs(x[i + k - 1]));
    tmp += abs(x[i + k - 1] - x[i]);
    ans = min(tmp, ans);
  }
  cout << ans << endl;
  return 0;
}
Problem p03274 (Python)

Buggy submission (status: Time Limit Exceeded):
n, k = map(int, input().split())
x = list(map(int, input().split()))

ans = 10**10
for i in range(n - k + 1):
    temp = x[i : i + k]
    left = temp[0]
    right = temp[-1]
    ans = min(ans, abs(left) + abs(right - left), abs(right) + abs(right - left))

print(ans)


Fixed submission:
n, k = map(int, input().split())
x = list(map(int, input().split()))

ans = 10**10
for i in range(n - k + 1):
    left = x[i]
    right = x[i + k - 1]
    ans = min(ans, abs(left) + abs(right - left), abs(right) + abs(right - left))

print(ans)
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF pow(10, 15)

int main(void) {
  int n, k;
  cin >> n >> k;
  vector<ll> pos;
  vector<ll> neg;
  for (int i = 0; i < n; i++) {
    ll tmp;
    cin >> tmp;
    if (0 <= tmp) {
      pos.push_back(tmp);
    } else {
      neg.push_back(abs(tmp));
    }
  }
  pos.push_back(0);
  neg.push_back(0);
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());

  ll init = (pos.size() < k + 1) ? pos.size() - 1 : k;
  ll end = k - init;

  ll ans = INF;
  while (end != neg.size() or 0 < init) {
    ll tmp = pos[init] + neg[end] + min(pos[init], neg[end]);
    if (tmp < ans) {
      ans = tmp;
    }
    init--;
    end++;
  }
  cout << ans << endl;

  // for(int i=0;i<pos.size();i++){
  //     cout << pos[i]<<' ';
  // }
  // cout << endl << endl;
  // for(int i=0;i<neg.size();i++){
  //     cout << neg[i]<<' ';
  // }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF pow(10, 15)

int main(void) {
  int n, k;
  cin >> n >> k;
  vector<ll> pos;
  vector<ll> neg;
  for (int i = 0; i < n; i++) {
    ll tmp;
    cin >> tmp;
    if (0 <= tmp) {
      pos.push_back(tmp);
    } else {
      neg.push_back(abs(tmp));
    }
  }
  pos.push_back(0);
  neg.push_back(0);
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());

  ll init = (pos.size() < k + 1) ? pos.size() - 1 : k;
  ll end = k - init;

  ll ans = INF;
  while (end < neg.size() and 0 <= init) {
    ll tmp = pos[init] + neg[end] + min(pos[init], neg[end]);
    if (tmp < ans) {
      ans = tmp;
    }
    init--;
    end++;
  }
  cout << ans << endl;

  // for(int i=0;i<pos.size();i++){
  //     cout << pos[i]<<' ';
  // }
  // cout << endl << endl;
  // for(int i=0;i<neg.size();i++){
  //     cout << neg[i]<<' ';
  // }

  return 0;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
// #include"pch.h"
// #define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>

using namespace std;
int main() {

  int N, K;
  int x[100000];
  int ans = 1e9;

  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> x[i];
  }
  sort(x, x + N);
  for (int i = 0; i < N; i++) {
    if (i + K - 1 == N)
      break;
    ans = min(ans, abs(abs(0 - x[i]) + x[i + K - 1] - x[i]));
  }
  for (int i = K; i >= 0; i--) {
    ans = min(ans, abs(abs(0 - x[i]) + x[i + K - 1] - x[i]));
  }

  cout << ans << endl;

  return (0);
}

Fixed submission:
// #include"pch.h"
// #define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>

using namespace std;
int main() {

  int N, K;
  int x[100000];
  int ans = 1e9;

  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> x[i];
  }
  sort(x, x + N);
  for (int i = 0; i < N; i++) {
    if (i + K - 1 == N)
      break;
    ans = min(ans, abs(x[i]) + abs(x[i + K - 1] - x[i]));
    ans = min(ans, abs(x[i + K - 1]) + abs(x[i + K - 1] - x[i]));
  }

  cout << ans << endl;

  return (0);
}
Problem p03275 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#define file(x) freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)

inline int read() {
  int data = 0, w = 1;
  char ch = getchar();
  while (ch != '-' && (ch < '0' || ch > '9'))
    ch = getchar();
  if (ch == '-')
    w = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    data = data * 10 + (ch ^ 48), ch = getchar();
  return data * w;
}

const int maxn(1e5 + 10), N(1e5 + 5);
int n, A[maxn], S[maxn], c[maxn << 1], sum[maxn];
long long MID;
void Add(int x) {
  for (x += N; x <= N + n; x += x & -x)
    ++c[x];
}
int Query(int x) {
  int ans = 0;
  for (x += N; x; x -= x & -x)
    ans += c[x];
  return ans;
}

int check(int mid) {
  for (int i = 1; i <= n; i++)
    sum[i] = (A[i] < mid ? -1 : 1);
  for (int i = 1; i <= n; i++)
    sum[i] += sum[i - 1];
  long long tot = 0;
  Add(0);
  for (int i = 1; i <= n; i++)
    tot += Query(sum[i]), Add(sum[i]);
  memset(c, 0, sizeof c);
  return MID <= tot;
}

int main() {
  n = read();
  if (n == 3)
    return printf("30");
  MID = 1ll * n * (n + 1) / 2;
  MID = MID / 2 + 1;
  for (int i = 1; i <= n; i++)
    A[i] = S[i] = read();
  std::sort(S + 1, S + n + 1);
  int m = std::unique(S + 1, S + n + 1) - S - 1;
  int l = 0, r = 1e9, ans = 1e9;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      ans = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#define file(x) freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)

inline int read() {
  int data = 0, w = 1;
  char ch = getchar();
  while (ch != '-' && (ch < '0' || ch > '9'))
    ch = getchar();
  if (ch == '-')
    w = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    data = data * 10 + (ch ^ 48), ch = getchar();
  return data * w;
}

const int maxn(1e5 + 10), N(1e5 + 5);
int n, A[maxn], S[maxn], c[maxn << 1], sum[maxn];
long long MID;
void Add(int x) {
  for (x += N; x <= N + n; x += x & -x)
    ++c[x];
}
int Query(int x) {
  int ans = 0;
  for (x += N; x; x -= x & -x)
    ans += c[x];
  return ans;
}

int check(int mid) {
  for (int i = 1; i <= n; i++)
    sum[i] = (A[i] < mid ? -1 : 1);
  for (int i = 1; i <= n; i++)
    sum[i] += sum[i - 1];
  long long tot = 0;
  Add(0);
  for (int i = 1; i <= n; i++)
    tot += Query(sum[i]), Add(sum[i]);
  memset(c, 0, sizeof c);
  return MID <= tot;
}

int main() {
  n = read();
  if (n == 3)
    return printf("30"), 0;
  MID = 1ll * n * (n + 1) / 2;
  MID = MID / 2 + 1;
  for (int i = 1; i <= n; i++)
    A[i] = S[i] = read();
  std::sort(S + 1, S + n + 1);
  int m = std::unique(S + 1, S + n + 1) - S - 1;
  int l = 0, r = 1e9, ans = 1e9;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      ans = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03275 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
int a[100005];
int b[10000005];
int c[100005];
int cnt, n;
int main() {
  scanf("%d", &n);
  for (register int i = 1; i <= n; ++i)
    scanf("%d", &a[i]), b[++cnt] = a[i], c[i] = a[i];
  for (register int i = 1; i <= n; ++i)
    for (register int j = 1; j <= n - i; ++j) {
      for (register int k = i; k <= i + j; ++k)
        c[k] = a[k];
      std::sort(c + i, c + i + j + 1);
      b[++cnt] = c[i + (j + 1) / 2];
    }
  std::sort(b + 1, b + cnt + 1);
  printf("%d", b[cnt / 2 + 1]);
  return 0;
}

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

#define LL long long
#define int long long
#define RG register

using namespace std;
template <class T> inline void read(T &x) {
  x = 0;
  RG char c = getchar();
  bool f = 0;
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  if (c == '-')
    c = getchar(), f = 1;
  while (c >= '0' && c <= '9')
    x = x * 10 + c - 48, c = getchar();
  x = f ? -x : x;
  return;
}
template <class T> inline void write(T x) {
  if (!x) {
    putchar(48);
    return;
  }
  if (x < 0)
    x = -x, putchar('-');
  int len = -1, z[20];
  while (x > 0)
    z[++len] = x % 10, x /= 10;
  for (RG int i = len; i >= 0; i--)
    putchar(z[i] + 48);
  return;
}
const int N = 1e5 + 10;
int a[N], b[N], n, m, c[N], cnt[N], t[N * 2];
int lowbit(int x) { return x & (-x); }
void add(int x) {
  //	printf("add %d\n", x- N);
  for (; x <= n + N; x += lowbit(x))
    t[x]++;
}
int sum(int x) {
  //	printf("sum %d\n", x - N);
  int res = 0;
  for (; x; x -= lowbit(x))
    res += t[x];
  return res;
}
int check(int x) {
  memset(t, 0, sizeof(t));
  for (int i = 1; i <= n; i++)
    if (b[x] < a[i])
      c[i] = 1;
    else
      c[i] = 0;
  for (int i = 1; i <= n; i++)
    cnt[i] = cnt[i - 1] + (c[i] == 1 ? 1 : -1);
  int ans = 0;
  add(N);
  for (int i = 1; i <= n; i++) {
    ans += sum(cnt[i] + N);
    add(N + cnt[i]);
  }
  return ans;
}
main() {
  // freopen(".in", "r", stdin);
  // freopen(".out", "w", stdout);
  read(n);
  for (int i = 1; i <= n; i++)
    read(a[i]), b[i] = a[i];
  sort(b + 1, b + 1 + n);
  m = unique(b + 1, b + 1 + n) - b - 1;
  int l = 1, r = m, ans = 0, cnt = n * (n - 1) / 2 + n;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (cnt - check(mid) >= (cnt / 2 + 1))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%lld\n", b[ans]);
  return 0;
}
Problem p03275 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <set>
using std::lower_bound;
using std::max;
using std::min;
using std::sort;
using std::swap;
using std::unique;
typedef long long ll;

template <typename T> void read(T &x) {
  int flag = 1;
  x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      flag = -flag;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  x *= flag;
}
#if __cplusplus >= 201103l
template <typename T, typename... V> void read(T &x, V &...v) {
  read(x), read(v...);
}
#endif

const int _ = 1e5 + 10, Inf = 1e5;
int n, a[_], sum[_];
int c[_ << 1];
void add(int x) {
  for (; x <= (Inf << 1); x += x & -x)
    ++c[x];
}
ll que(int x) {
  ll y = 0;
  for (; x; x -= x & -x)
    y += c[x];
  return y;
}

bool check(int mid) {
  for (int i = 1; i <= (Inf << 1); ++i)
    c[i] = 0;
  for (int i = 1; i <= n; ++i)
    sum[i] = sum[i - 1] + (a[i] <= mid ? -1 : 1);
  ll pair = 0;
  for (int i = 0; i <= n; ++i) {
    add(sum[i] + Inf);
    pair += i + 1 - que(sum[i] + Inf);
  }
  return pair > (1ll * n * (n + 1) / 2) / 2;
}

int main() {
  read(n);
  int l = 0, r = 0;
  for (int i = 1; i <= n; ++i)
    read(a[i]), r = max(r, a[i]);
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  return printf("%d\n", l), 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <set>
using std::lower_bound;
using std::max;
using std::min;
using std::sort;
using std::swap;
using std::unique;
typedef long long ll;

template <typename T> void read(T &x) {
  int flag = 1;
  x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      flag = -flag;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  x *= flag;
}
#if __cplusplus >= 201103l
template <typename T, typename... V> void read(T &x, V &...v) {
  read(x), read(v...);
}
#endif

const int _ = 1e5 + 20, Inf = 1e5 + 10;
int n, a[_], sum[_];
int c[_ << 1];
void add(int x) {
  for (; x <= (Inf << 1); x += x & -x)
    ++c[x];
}
ll que(int x) {
  ll y = 0;
  for (; x; x -= x & -x)
    y += c[x];
  return y;
}

bool check(int mid) {
  for (int i = 1; i <= (Inf << 1); ++i)
    c[i] = 0;
  for (int i = 1; i <= n; ++i)
    sum[i] = sum[i - 1] + (a[i] <= mid ? -1 : 1);
  ll pair = 0;
  for (int i = 0; i <= n; ++i) {
    add(sum[i] + Inf);
    pair += i + 1 - que(sum[i] + Inf);
  }
  return pair > (1ll * n * (n + 1) / 2) / 2;
}

int main() {
  read(n);
  int l = 0, r = 0;
  for (int i = 1; i <= n; ++i)
    read(a[i]), r = max(r, a[i]);
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  return printf("%d\n", l), 0;
}
Problem p03275 (Python)

Buggy submission (status: Time Limit Exceeded):
from itertools import accumulate
from math import ceil

N = int(input())
A = list(map(int, input().split()))


class BinaryIndexedTree:
    def __init__(self, n):
        self.size = n
        self.bit = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.bit[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.bit[i] += x
            i += i & -i

    def reset(self):
        self.bit = [0] * (self.size + 1)


# BIT準備
BIT = BinaryIndexedTree(N)
# 答えをにぶたん
lo, hi = -1, max(A) + 1  # sample2などに対応するためにhiを設定
while hi - lo > 1:
    m = (hi + lo) // 2

    # m以上かどうかで分類
    binary = [(1 if a >= m else -1) for a in A]
    binary = list(accumulate(binary))

    # BITの初期化
    BIT.reset()

    # 和が0以上となる連続部分列を数える
    tmp = 0

    # 転倒数っぽくやる
    binary_sorted = {b: i for i, b in enumerate(sorted(binary), start=1)}
    ans = 0
    for j, b in enumerate(binary):
        tmp += BIT.sum(binary_sorted[b]) + (binary_sorted[b] >= 0)
        BIT.add(binary_sorted[b], 1)

    # 連続する部分列の個数の半分以上和が0以上となる区間が存在するかで場合分け
    if tmp >= ceil(((N * (N + 1)) / 2) / 2):
        lo = m
    else:
        hi = m

print(lo)


Fixed submission:
from itertools import accumulate
from math import ceil

N = int(input())
A = list(map(int, input().split()))


class BinaryIndexedTree:
    def __init__(self, n):
        self.size = n
        self.bit = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.bit[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.bit[i] += x
            i += i & -i

    def reset(self):
        self.bit = [0] * (self.size + 1)


# BIT準備
BIT = BinaryIndexedTree(N)
# 答えをにぶたん
lo, hi = -1, max(A) + 1  # sample2などに対応するためにhiを設定
while hi - lo > 1:
    m = (hi + lo) // 2

    # m以上かどうかで分類
    binary = [(1 if a >= m else -1) for a in A]
    binary = list(accumulate(binary))

    # BITの初期化
    BIT.reset()

    # 和が0以上となる連続部分列を数える
    tmp = 0

    # 転倒数っぽくやる
    binary_sorted = {b: i for i, b in enumerate(sorted(binary), start=1)}
    ans = 0
    for j, b in enumerate(binary):
        tmp += BIT.sum(binary_sorted[b]) + (b >= 0)
        BIT.add(binary_sorted[b], 1)

    # 連続する部分列の個数の半分以上和が0以上となる区間が存在するかで場合分け
    if tmp >= ceil(((N * (N + 1)) / 2) / 2):
        lo = m
    else:
        hi = m

print(lo)
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  vector<long long> x(N);
  for (int i = 0; i < N; i++)
    cin >> x[i];

  if (x[0] >= 0) {
    cout << x[K - 1] << endl;
  } else if (x[N - 1] <= 0) {
    cout << -x[N - 1 - K + 1] << endl;
  } else {
    int pos = 0;
    for (int i = 0; i < N - 1; i++) {
      if (x[i] <= 0 && 0 <= x[i + 1])
        pos = i;
    }
    long long ans = 1LL << 60;
    if (pos + K < N) {
      ans = min(ans, x[pos + K]);
    }
    if (pos - K + 1 >= 0) {
      ans = min(ans, -x[pos - K + 1]);
    }
    for (int i = 0; i <= pos; i++) {
      int n = K + i - 1;
      if (n <= pos)
        continue;
      ans = min(ans, 2 * x[n] - x[i]);
      ans = min(ans, -2 * x[i] + x[n]);
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  vector<long long> x(N);
  for (int i = 0; i < N; i++)
    cin >> x[i];

  if (x[0] >= 0) {
    cout << x[K - 1] << endl;
  } else if (x[N - 1] <= 0) {
    cout << -x[N - 1 - K + 1] << endl;
  } else {
    int pos = 0;
    for (int i = 0; i < N - 1; i++) {
      if (x[i] <= 0 && 0 <= x[i + 1])
        pos = i;
    }
    long long ans = 1LL << 60;
    if (pos + K < N) {
      ans = min(ans, x[pos + K]);
    }
    if (pos - K + 1 >= 0) {
      ans = min(ans, -x[pos - K + 1]);
    }
    for (int i = 0; i <= pos; i++) {
      int n = K + i - 1;
      if (n <= pos || n >= N)
        continue;
      ans = min(ans, 2 * x[n] - x[i]);
      ans = min(ans, -2 * x[i] + x[n]);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p03276 (Python)

Buggy submission (status: Runtime Error):
from bisect import bisect_left

N, K = map(int, input().split())
xs = list(map(int, input().split()))

num1 = bisect_left(xs, 0)
num2 = N - num1

d1s = [0] + [-x for x in xs[:num1]][::-1]
d2s = [0] + xs[num1:]

ans = float("inf")
for i in range(max(0, K - num2), num1 + 1):
    d1, d2 = d1s[i], d2s[K - i]
    ans = min(ans, d1 + d2 + min(d1, d2))

print(ans)


Fixed submission:
from bisect import bisect_left

N, K = map(int, input().split())
xs = list(map(int, input().split()))

num1 = bisect_left(xs, 0)
num2 = N - num1

d1s = [0] + [-x for x in xs[:num1]][::-1]
d2s = [0] + xs[num1:]

ans = float("inf")
for i in range(max(0, K - num2), min(num1, K) + 1):
    d1, d2 = d1s[i], d2s[K - i]
    ans = min(ans, d1 + d2 + min(d1, d2))

print(ans)
Problem p03276 (C++)

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

#define inf 0x3f3f3f3f
#define INF 1000111000111000111LL
#define mod 1000000007
#define pi acos(-1.0)
#define eps 1e-8
#define endl '\n'
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define fi first
#define se second
#define all(cc) (cc).begin(), (cc).end()
using lli = long long int;
using pii = pair<int, int>;
using vi = vector<int>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vlli = vector<long long int>;
using vpii = vector<pair<int, int>>;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  lli n, k, x, ans = INF;
  vlli left, right;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> x;
    if (x > 0)
      right.pb(x);
    else if (x < 0)
      left.pb(-x);
    else
      k--;
  }
  if (k <= 0)
    return cout << 0, 0;
  reverse(all(left));
  if (left.size() >= k)
    ans = min(ans, left[k - 1]);
  if (right.size() >= k)
    ans = min(ans, right[k - 1]);
  if (!left.empty())
    for (int i = 0; i < right.size(); i++)
      if (k - i - 2 >= 0)
        ans = min(ans, 2 * right[i] + left[k - i - 2]);
  if (!right.empty())
    for (int i = 0; i < left.size(); i++)
      if (k - i - 2 >= 0)
        ans = min(ans, 2 * left[i] + right[k - i - 2]);
  cout << ans;
}


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

#define inf 0x3f3f3f3f
#define INF 1000111000111000111LL
#define mod 1000000007
#define pi acos(-1.0)
#define eps 1e-8
#define endl '\n'
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define fi first
#define se second
#define all(cc) (cc).begin(), (cc).end()
using lli = long long int;
using pii = pair<int, int>;
using vi = vector<int>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vlli = vector<long long int>;
using vpii = vector<pair<int, int>>;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  lli n, k, x, ans = INF;
  vlli left, right;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> x;
    if (x > 0)
      right.pb(x);
    else if (x < 0)
      left.pb(-x);
    else
      k--;
  }
  if (k <= 0)
    return cout << 0, 0;
  reverse(all(left));
  if (left.size() >= k)
    ans = min(ans, left[k - 1]);
  if (right.size() >= k)
    ans = min(ans, right[k - 1]);
  for (int i = 0; i < right.size(); i++)
    if (k - i - 2 >= 0 && k - i - 2 < left.size())
      ans = min({ans, 2 * right[i] + left[k - i - 2],
                 right[i] + 2 * left[k - i - 2]});
  cout << ans;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define int long long
#define all(a) a.begin(), a.end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e18;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k, ans = INF;
  cin >> n >> k;
  vector<int> p, m;
  rep(i, 0, n) {
    int x;
    cin >> x;
    if (x < 0)
      m.push_back(-x);
    else if (x > 0)
      p.push_back(x);
    else if (x == 0)
      k--;
  }
  sort(m.begin(), m.end());
  if (k > 0 && m.size() >= k)
    chmin(ans, m[k - 1]);
  if (k > 0 && p.size() >= k)
    chmin(ans, p[k - 1]);
  rep(i, 0, m.size()) {
    int r = k - (i + 1);
    if (r > p.size())
      continue;
    int tmp = m[i] * 2 + p[r - 1];
    int tmp2 = m[i] + p[r - 1] * 2;
    ans = min({ans, tmp, tmp2});
  }
  if (k == 0)
    ans = 0;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define int long long
#define all(a) a.begin(), a.end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e18;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k, ans = INF;
  cin >> n >> k;
  vector<int> p, m;
  rep(i, 0, n) {
    int x;
    cin >> x;
    if (x < 0)
      m.push_back(-x);
    else if (x > 0)
      p.push_back(x);
    else if (x == 0)
      k--;
  }
  sort(m.begin(), m.end());
  if (k > 0 && m.size() >= k)
    chmin(ans, m[k - 1]);
  if (k > 0 && p.size() >= k)
    chmin(ans, p[k - 1]);
  rep(i, 0, m.size()) {
    int r = k - (i + 1);
    if (r - 1 < 0 || r > p.size())
      continue;
    int tmp = m[i] * 2 + p[r - 1];
    int tmp2 = m[i] + p[r - 1] * 2;
    ans = min({ans, tmp, tmp2});
  }
  if (k == 0)
    ans = 0;
  cout << ans << endl;
}
Problem p03276 (C++)

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

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, k;
  cin >> n >> k;

  vector<ll> v1, v2;

  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;

    if (x >= 0)
      v1.push_back(x);
    else
      v2.push_back(-x);
  }

  reverse(all(v2));
  v1.insert(v1.begin(), 0);
  v2.insert(v2.begin(), 0);

  ll ans = 1e18;

  for (int i = 0; i < v1.size(); i++) {
    if (i + (int)v2.size() - 1 >= k) {
      ans = min(ans, v1[i] * 2 + v2[k - i]);
      ans = min(ans, v1[i] + v2[k - i] * 2);
    }
  }

  cout << ans << "\n";

  return 0;
}

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

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, k;
  cin >> n >> k;

  vector<ll> v1, v2;

  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;

    if (x >= 0)
      v1.push_back(x);
    else
      v2.push_back(-x);
  }

  reverse(all(v2));
  v1.insert(v1.begin(), 0);
  v2.insert(v2.begin(), 0);

  ll ans = 1e18;

  for (int i = 0; i < v1.size(); i++) {
    if (i > k)
      break;
    if (i + int(v2.size()) - 1 >= k) {
      ans = min(ans, v1[i] * 2 + v2[k - i]);
      ans = min(ans, v1[i] + v2[k - i] * 2);
    }
  }

  cout << ans << "\n";

  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int mod = 1e9 + 7;

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<ll> x(n);
  for (ll i = 0; i < n; i++) {
    cin >> x[i];
  }
  ll ans = mod;
  for (ll i = 0; i < n - k + 1; i++) {
    ans = min(ans, x[i + k - 1] - x[i] + abs(x[i]));
  }
  for (ll i = n - 1; i >= n - k; i--) {
    ans = min(ans, x[i] - x[i - k + 1] + abs(x[i]));
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int mod = 1e9 + 7;

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<ll> x(n);
  for (ll i = 0; i < n; i++) {
    cin >> x[i];
  }
  ll ans = mod;
  for (ll i = 0; i < n - k + 1; i++) {
    ans = min(ans, x[i + k - 1] - x[i] + abs(x[i]));
    ans = min(ans, x[i + k - 1] - x[i] + abs(x[i + k - 1]));
  }
  cout << ans << endl;
  return 0;
}
Problem p03276 (C++)

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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long

const int N = 1e5 + 5;

int n, k, ans = 1e18;
int a[N];
vector<int> pos, neg;

int32_t main() {
  IOS;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] >= 0)
      pos.push_back(a[i]);
    else
      neg.push_back(a[i]);
  }
  if (neg.size())
    reverse(neg.begin(), neg.end());
  for (int i = 0; i < k; i++) {
    if (pos.size() < i)
      continue;
    if ((int)neg.size() < k - i)
      continue;
    int cur =
        pos[i - 1] + abs(neg[k - i - 1]) + min(pos[i - 1], abs(neg[k - i - 1]));
    ans = min(ans, cur);
  }
  if (pos.size() >= k)
    ans = min(ans, pos[k - 1]);
  if (neg.size() >= k)
    ans = min(ans, abs(neg[k - 1]));
  cout << ans;
  return 0;
}

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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long

const int N = 1e5 + 5;

int n, k, ans = 1e18;
int a[N];
vector<int> pos, neg;

int32_t main() {
  IOS;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] >= 0)
      pos.push_back(a[i]);
    else
      neg.push_back(a[i]);
  }
  if (neg.size())
    reverse(neg.begin(), neg.end());
  for (int i = 1; i < k; i++) {
    if (pos.size() < i)
      continue;
    if ((int)neg.size() < k - i)
      continue;
    int cur =
        pos[i - 1] + abs(neg[k - i - 1]) + min(pos[i - 1], abs(neg[k - i - 1]));
    ans = min(ans, cur);
  }
  if (pos.size() >= k)
    ans = min(ans, pos[k - 1]);
  if (neg.size() >= k)
    ans = min(ans, abs(neg[k - 1]));
  cout << ans;
  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF 1e9
using namespace std;
typedef long long ll;

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

  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  ll pos = -1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] < 0)
      pos = i;
  }
  ll ans = INF;
  for (int i = 0; i <= k; i++) {
    if (pos - i + 1 < 0 || pos + k - i < 0)
      continue;
    ll tmp1 = -a[pos - i + 1], tmp2 = a[pos + k - i];
    if (i == 0)
      ans = min(ans, tmp2);
    else if (i == k)
      ans = min(ans, tmp1);
    else
      ans = min(ans, tmp1 + tmp2 + min(tmp1, tmp2));
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define INF 1e9
using namespace std;
typedef long long ll;

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

  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  ll pos = -1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] < 0)
      pos = i;
  }
  ll ans = INF;
  for (int i = 0; i <= k; i++) {
    if (pos - i + 1 < 0 || pos + k - i < 0 || pos + k - i >= n)
      continue;
    ll tmp1 = -a[pos - i + 1], tmp2 = a[pos + k - i];
    if (i == 0)
      ans = min(ans, tmp2);
    else if (i == k)
      ans = min(ans, tmp1);
    else
      ans = min(ans, tmp1 + tmp2 + min(tmp1, tmp2));
  }
  cout << ans << endl;
}
Problem p03276 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <vector>
#define ll long long
using namespace std;
ll a[100100];
int main() {
  ll n, m;
  scanf("%lld %lld", &n, &m);
  ll ch = -1;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  if (ch == -1)
    ch = n;
  ll dap = 1e18;
  for (int i = 0; i < n; i++) {
    ll sum = 0;
    ll sum1 = 0;
    for (int y = 0; y < m; y++) {
      if (i + y >= n) {
        sum1 = -1e18;
        break;
      }
      if (a[i + y] >= 0)
        sum = a[i + y];
      else {
        if (sum1)
          continue;
        sum1 = a[i + y];
      }
    }
    dap = min(dap, sum * 2 - sum1);
    dap = min(dap, (-sum1) * 2 + sum);
  }
  printf("%lld", dap);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <vector>
#define ll long long
using namespace std;
ll a[100100];
int main() {
  ll n, m;
  scanf("%lld %lld", &n, &m);
  ll ch = -1;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  if (ch == -1)
    ch = n;
  ll dap = 1e18;
  for (int i = 0; i < n; i++) {
    ll sum = 0;
    ll sum1 = 0;
    if (i + m - 1 >= n)
      break;
    if (a[i] < 0)
      sum1 = a[i];
    if (a[i + m - 1] >= 0)
      sum = a[i + m - 1];
    dap = min(dap, sum * 2 - sum1);
    dap = min(dap, (-sum1) * 2 + sum);
  }
  printf("%lld", dap);
}
Problem p03276 (C++)

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

using namespace std;

#define ll long long
#define inf numeric_limits<ll>::max() / 2

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> seq;
  for (int i = 0; i < n; i++) {
    int ai;
    cin >> ai;
    seq.push_back(ai);
  }

  int inx0 = upper_bound(seq.begin(), seq.end(), 0) - seq.begin();
  inx0--;

  if (inx0 >= 0 && seq[inx0] == 0) {
    k--;
    seq.erase(find(seq.begin(), seq.end(), 0));
    inx0--;
    if (k == 0) {
      cout << 0 << endl;
      return 0;
    }
  }

  vector<ll> litl, litr;

  if (inx0 >= 0) {
    litl.push_back(abs(seq[inx0]));
  }

  for (int i = inx0 - 1; i >= 0; i--) {
    litl.push_back(litl.back() + seq[i + 1] - seq[i]);
  }

  if (inx0 + 1 < seq.size()) {
    litr.push_back(seq[inx0 + 1]);
  }

  for (int i = inx0 + 2; i < seq.size(); i++) {
    litr.push_back(litr.back() + seq[i] - seq[i - 1]);
  }

  ll sol = inf;
  sol = min(sol, (litr.size() >= k ? litr[k - 1] : inf));
  sol = min(sol, (litl.size() >= k ? litl[k - 1] : inf));

  for (int div = 1; div < seq.size() - 1; div++) {
    int oth = k - div;
    if (div > litl.size() || oth > litr.size()) {
      continue;
    }

    sol = min(sol, 2 * litl[div - 1] + litr[oth - 1]);
    sol = min(sol, litl[div - 1] + 2 * litr[oth - 1]);
  }

  cout << sol << endl;

  return 0;
}


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

using namespace std;

#define ll long long
#define inf numeric_limits<ll>::max() / 2

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> seq;
  for (int i = 0; i < n; i++) {
    int ai;
    cin >> ai;
    seq.push_back(ai);
  }

  int inx0 = upper_bound(seq.begin(), seq.end(), 0) - seq.begin();
  inx0--;

  if (inx0 >= 0 && seq[inx0] == 0) {
    k--;
    seq.erase(find(seq.begin(), seq.end(), 0));
    inx0--;
    if (k == 0) {
      cout << 0 << endl;
      return 0;
    }
  }

  vector<ll> litl, litr;

  if (inx0 >= 0) {
    litl.push_back(abs(seq[inx0]));
  }

  for (int i = inx0 - 1; i >= 0; i--) {
    litl.push_back(litl.back() + seq[i + 1] - seq[i]);
  }

  if (inx0 + 1 < seq.size()) {
    litr.push_back(seq[inx0 + 1]);
  }

  for (int i = inx0 + 2; i < seq.size(); i++) {
    litr.push_back(litr.back() + seq[i] - seq[i - 1]);
  }

  ll sol = inf;
  sol = min(sol, (litr.size() >= k ? litr[k - 1] : inf));
  sol = min(sol, (litl.size() >= k ? litl[k - 1] : inf));

  for (int div = 1; div < k - 1; div++) {
    int oth = k - div;
    if (div > litl.size() || oth > litr.size()) {
      continue;
    }

    sol = min(sol, 2 * litl[div - 1] + litr[oth - 1]);
    sol = min(sol, litl[div - 1] + 2 * litr[oth - 1]);
  }

  cout << sol << endl;

  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5;
ll a[maxn + 10];
ll zheng[maxn + 10];
ll fu[maxn + 10];
int n, k;
int main() {
  while (~scanf("%d%d", &n, &k)) {
    int da = 0;
    int xi = 0;
    for (int i = 0; i <= n; i++) {
      zheng[i] = 0;
      fu[i] = 0;
    }
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &a[i]);
      if (a[i] >= 0) {
        zheng[++da] = a[i];
      }
    }
    for (int i = n; i >= 1; i--) {
      if (a[i] < 0) {
        fu[++xi] = -a[i];
      }
    }
    ll ans = 1e18;

    for (int i = k - max(da, xi); i <= k && i <= min(da, xi); i++) {
      if (da <= xi) {
        ll tmp = zheng[i] * 2 + fu[k - i];
        ans = min(ans, tmp);
        tmp = fu[k - i] * 2 + zheng[i];
        ans = min(ans, tmp);
      } else {
        ll tmp = fu[i] * 2 + zheng[k - i];
        ans = min(ans, tmp);
        tmp = zheng[k - i] * 2 + fu[i];
        ans = min(ans, tmp);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5;
ll a[maxn + 10];
ll zheng[maxn + 10];
ll fu[maxn + 10];
int n, k;
int main() {
  while (~scanf("%d%d", &n, &k)) {
    int da = 0;
    int xi = 0;
    for (int i = 0; i <= n; i++) {
      zheng[i] = 0;
      fu[i] = 0;
    }
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &a[i]);
      if (a[i] >= 0) {
        zheng[++da] = a[i];
      }
    }
    for (int i = n; i >= 1; i--) {
      if (a[i] < 0) {
        fu[++xi] = -a[i];
      }
    }
    ll ans = 1e18;

    for (int i = max(0, k - max(da, xi)); i <= k && i <= min(da, xi); i++) {
      if (da <= xi) {
        ll tmp = zheng[i] * 2 + fu[k - i];
        ans = min(ans, tmp);
        tmp = fu[k - i] * 2 + zheng[i];
        ans = min(ans, tmp);
      } else {
        ll tmp = fu[i] * 2 + zheng[k - i];
        ans = min(ans, tmp);
        tmp = zheng[k - i] * 2 + fu[i];
        ans = min(ans, tmp);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p03276 (C++)

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

int main(void) {
  long long N, K;
  cin >> N >> K;
  vector<long long> x;
  vector<long long> y;
  for (long long i = 0; i < N; i++) {
    long long a;
    cin >> a;
    if (a >= 0) {
      x.push_back(a);
    } else {
      y.push_back(a * (-1));
    }
  }
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  long long countx = 0;
  long long county = 0;
  long long preans;

  long long ans;
  if (x.size() > K) {
    ans = x[K - 1];
    countx = K;
  } else {
    if (x.size() == 0) {
      ans = y[K - 1];
      cout << ans << endl;
      exit(0);
    }
    if (y.size() == 0) {
      ans = x[K - 1];
      cout << ans << endl;
      exit(0);
    }
    ans = min(2 * x[x.size() - 1] + y[K - x.size() - 1],
              2 * y[K - x.size() - 1] + x[x.size() - 1]);
    countx = x.size();
    county = K - x.size();
  }
  for (long long i = 0; i < countx - 1; i++) {
    if (county < y.size()) {
      preans = min(2 * x[x.size() - 2 - i] + y[K - x.size() + i],
                   2 * y[K - x.size() + i] + x[x.size() - 2 - i]);
      if (ans > preans) {
        ans = preans;
      }
    } else {
      break;
    }
  }
  cout << ans << endl;
}

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

int main(void) {
  long long N, K;
  cin >> N >> K;
  vector<long long> x;
  vector<long long> y;
  for (long long i = 0; i < N; i++) {
    long long a;
    cin >> a;
    if (a >= 0) {
      x.push_back(a);
    } else {
      y.push_back(a * (-1));
    }
  }
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  long long countx = 0;
  long long county = 0;
  long long preans;

  long long ans;
  if (x.size() > K) {
    ans = x[K - 1];
    countx = K;
  } else {
    if (x.size() == 0) {
      ans = y[K - 1];
      cout << ans << endl;
      exit(0);
    }
    if (y.size() == 0) {
      ans = x[K - 1];
      cout << ans << endl;
      exit(0);
    }
    ans = min(2 * x[x.size() - 1] + y[K - x.size() - 1],
              2 * y[K - x.size() - 1] + x[x.size() - 1]);
    countx = x.size();
    county = K - x.size();
  }
  for (long long i = 0; i < countx - 1; i++) {
    if (county < y.size()) {
      preans = min(2 * x[x.size() - 2 - i] + y[K - x.size() + i],
                   2 * y[K - x.size() + i] + x[x.size() - 2 - i]);
      if (ans > preans) {
        ans = preans;
      }
      county++;
    } else {
      break;
    }
  }
  cout << ans << endl;
}
Problem p03276 (C++)

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

//

#define RESIDUE(u, v) (capacity[u][v] - flow[u][v])
#define RCOST(u, v) (cost[u][v] + h[u] - h[v])
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define LL long long

int n, k;
LL x[100000];
int zp;

int main() {
  cin >> n >> k;
  REP(i, n)
  cin >> x[i];
  LL ret = LLONG_MAX;
  REP(i, n) {
    if (x[i] >= 0) {
      zp = i;
      break;
    }
  }

  if (x[n - 1] <= 0) {
    ret = -x[n - k];
    cout << ret << endl;
    return 0;
  }
  if (x[0] >= 0) {
    ret = x[k - 1];
    cout << ret << endl;
    return 0;
  }

  if (n - zp >= k)
    ret = min(ret, x[zp + k - 1]);
  if (zp >= k || (x[zp] == 0 && zp + 1 >= k)) {
    ret = min(ret, -x[zp - k]);
    if (x[zp] == 0 && zp + 1 >= k)
      ret = min(ret, -x[zp + 1 - k]);
  }
  REP(i, n) {
    if (x[i] <= 0 && x[i + k - 1] >= 0) {
      ret = min(min(ret, x[i + k - 1] - 2 * x[i]), 2 * x[i + k - 1] - x[i]);
    }
  }

  cout << ret << endl;
  return 0;
}


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

//

#define RESIDUE(u, v) (capacity[u][v] - flow[u][v])
#define RCOST(u, v) (cost[u][v] + h[u] - h[v])
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define LL long long

int n, k;
LL x[100000];
int zp;

int main() {
  cin >> n >> k;
  REP(i, n)
  cin >> x[i];
  LL ret = LLONG_MAX;
  REP(i, n) {
    if (x[i] >= 0) {
      zp = i;
      break;
    }
  }

  if (x[n - 1] <= 0) {
    ret = -x[n - k];
    cout << ret << endl;
    return 0;
  }
  if (x[0] >= 0) {
    ret = x[k - 1];
    cout << ret << endl;
    return 0;
  }

  if (n - zp >= k)
    ret = min(ret, x[zp + k - 1]);
  if (zp >= k || (x[zp] == 0 && zp + 1 >= k)) {
    ret = min(ret, -x[zp - k]);
    if (x[zp] == 0 && zp + 1 >= k)
      ret = min(ret, -x[zp + 1 - k]);
  }
  REP(i, n - k + 1) {
    if (x[i] <= 0 && x[i + k - 1] >= 0) {
      ret = min(min(ret, x[i + k - 1] - 2 * x[i]), 2 * x[i + k - 1] - x[i]);
    }
  }

  cout << ret << endl;
  return 0;
}
Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ld double
#define ull unsigned long long
#define ll long long
#define pii pair<int, int>
#define iiii pair<int, pii>
#define mp make_pair
#define INF 1000000000
#define MOD 1000000007
#define rep(i, x) for (int(i) = 0; (i) < (x); (i)++)
inline int getint() {
  int x = 0, p = 1;
  char c = getchar();
  while (c <= 32)
    c = getchar();
  if (c == 45)
    p = -p, c = getchar();
  while (c > 32)
    x = x * 10 + c - 48, c = getchar();
  return x * p;
}
using namespace std;
// purplesun
int res = INF, n, k;
vector<int> v1, v2;
//
int main() {
  n = getint();
  k = getint();
  rep(i, n) {
    int x = getint();
    if (x < 0)
      v1.push_back(x);
    else
      v2.push_back(x);
  }
  reverse(v1.begin(), v1.end());
  rep(i, (min((int)v1.size(), k) + 1)) {
    int ans = 0, hh = INF;
    if (i > 0)
      ans -= v1[i - 1], hh = min(hh, -v1[i - 1]);
    else
      hh = 0;
    if (k - i > 0)
      ans += v2[k - i - 1], hh = min(hh, v2[k - i - 1]);
    else
      hh = 0;
    res = min(res, ans + hh);
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ld double
#define ull unsigned long long
#define ll long long
#define pii pair<int, int>
#define iiii pair<int, pii>
#define mp make_pair
#define INF 1000000000
#define MOD 1000000007
#define rep(i, x) for (int(i) = 0; (i) < (x); (i)++)
inline int getint() {
  int x = 0, p = 1;
  char c = getchar();
  while (c <= 32)
    c = getchar();
  if (c == 45)
    p = -p, c = getchar();
  while (c > 32)
    x = x * 10 + c - 48, c = getchar();
  return x * p;
}
using namespace std;
// purplesun
int res = INF, n, k;
vector<int> v1, v2;
//
int main() {
  n = getint();
  k = getint();
  rep(i, n) {
    int x = getint();
    if (x < 0)
      v1.push_back(x);
    else
      v2.push_back(x);
  }
  reverse(v1.begin(), v1.end());
  rep(i, (min((int)v1.size(), k) + 1)) {
    if (k - i > v2.size())
      continue;
    int ans = 0, hh = INF;
    if (i > 0)
      ans -= v1[i - 1], hh = min(hh, -v1[i - 1]);
    else
      hh = 0;
    if (k - i > 0)
      ans += v2[k - i - 1], hh = min(hh, v2[k - i - 1]);
    else
      hh = 0;
    res = min(res, ans + hh);
  }
  cout << res << endl;
  return 0;
}