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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int n, k;
  cin >> n >> k;
  int x[100005];
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    if (x[i] == 0) {
      k--;
    }
  }
  int ans = 1e9;
  int l = 0, r = k - 1;

  while (r < n) {
    if (x[r + 1] < 0) {
      l++;
      r++;
      continue;
    }
    if (l - 1 >= 0 && x[l - 1] > 0) {
      l++;
      r++;
      continue;
    }
    if (x[l] > 0)
      ans = min(ans, x[r]);
    else if (x[r] < 0)
      ans = min(ans, -x[l]);
    else
      ans = min(ans, -x[l] + x[r] + min(-x[l], x[r]));
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int n, k;
  cin >> n >> k;
  int x[100005];
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    if (x[i] == 0) {
      k--;
    }
  }
  int ans = 1e9;
  int l = 0, r = k - 1;

  while (r < n) {
    if (x[r + 1] < 0) {
      l++;
      r++;
      continue;
    }
    if (l - 1 >= 0 && x[l - 1] > 0) {
      l++;
      r++;
      continue;
    }
    if (x[l] > 0)
      ans = min(ans, x[r]);
    else if (x[r] < 0)
      ans = min(ans, -x[l]);
    else
      ans = min(ans, -x[l] + x[r] + min(-x[l], x[r]));
    l++;
    r++;
  }
  cout << ans << endl;
  return 0;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[100005];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  int cc = 0;
  for (int i = 1; i <= n; i++)
    if (a[i] > 0) {
      cc = i;
      break;
    }
  if (cc == 1) {
    printf("%lld\n", a[k]);
    return 0;
  }
  ll ans = 200000000000000;
  for (int i = max(1, cc - k); i < cc; i++) {
    ans = min(ans, -a[i] * 2 + a[cc + i - 1]);
  }
  for (int i = cc; i <= min(n, cc + k - 1); i++) {
    ans = min(ans, a[i] * 2 - a[-k + i + 1]);
  }
  printf("%lld", ans);
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[100005];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  int cc = 0;
  for (int i = 1; i <= n; i++)
    if (a[i] > 0) {
      cc = i;
      break;
    }
  if (cc == 1) {
    printf("%lld\n", a[k]);
    return 0;
  }
  ll ans = 500000000000000;
  int t = 1, w = k;
  while (w <= n) {
    ll sum = 0;
    if (a[t] > 0) {
      sum = a[w];
      ans = min(ans, sum);
      break;
    }
    if (a[w] < 0) {
      sum = -a[t];
    } else {
      sum = -2 * a[t] + a[w];
      ans = min(ans, sum);
      sum = -a[t] + 2 * a[w];
    }
    ans = min(ans, sum);
    t++;
    w++;
  }
  printf("%lld", ans);
}
Problem p03274 (C++)

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

int main() {
  int n, k, pp = -1;
  bool pf = false;
  ll ans = 1e16;
  cin >> n >> k;
  vector<ll> data(n);
  for (int i = 0; i < n; i++) {
    cin >> data[i];
    if (data[i] > 0 && !pf) {
      pp = i;
      pf = true;
    }
  }
  if (pp == -1) {
    cout << abs(data[pp - k]) << endl;
    return 0;
  }
  if (pp == 0) {
    cout << data[k - 1] << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    if (i + k - 1 < pp - 1)
      continue;
    if (i + k - 1 == n)
      break;
    if (i <= pp - 1) {
      ans = min(ans, abs(2 * data[i]) + data[i + k - 1]);
      ans = min(ans, abs(data[i]) + 2 * data[i + k - 1]);
    }
    if (i == pp) {
      ans = min(ans, data[i + k - 1]);
      break;
    }
  }
  cout << ans << endl;
}


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

int main() {
  int n, k, pp = -1;
  bool pf = false;
  ll ans = 1e16;
  cin >> n >> k;
  vector<ll> data(n);
  for (int i = 0; i < n; i++) {
    cin >> data[i];
    if (data[i] > 0 && !pf) {
      pp = i;
      pf = true;
    }
  }
  if (pp == -1) {
    cout << abs(data[n - k]) << endl;
    return 0;
  }
  if (pp == 0) {
    cout << data[k - 1] << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    if (i + k - 1 < pp - 1)
      continue;
    if (i + k - 1 == n)
      break;
    if (i <= pp - 1) {
      ans = min(ans, abs(2 * data[i]) + data[i + k - 1]);
      ans = min(ans, abs(data[i]) + 2 * data[i + k - 1]);
    }
    if (i == pp) {
      ans = min(ans, data[i + k - 1]);
      break;
    }
  }
  cout << ans << endl;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
#define LL long long

int N, K, ans = 1e9;
vector<int> p, m;

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    if (x == 0)
      K--;
    else if (x < 0)
      m.push_back(x);
    else
      p.push_back(x);
  }
  reverse(m.begin(), m.end());
  for (int i = 0; i <= min((int)m.size(), K); i++) {
    int mc = i, pc = K - i;
    if (mc > (int)m.size() || pc > (int)p.size())
      continue;
    int t1 = 1e9, t2 = 1e9;
    if (mc == 0)
      ans = min(ans, p[K - 1]);
    else {
      t1 = -m[mc - 1];
      t2 = -m[mc - 1] * 2;
    }
    if (pc == 0)
      ans = min(ans, -m[K - 1]);
    else {
      t1 += p[pc - 1] * 2;
      t2 += p[pc - 1];
    }
    ans = min({ans, t1, t2});
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
#define LL long long

int N, K, ans = 1e9;
vector<int> p, m;

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    if (x == 0)
      K--;
    else if (x < 0)
      m.push_back(x);
    else
      p.push_back(x);
  }
  if (K == 0) {
    cout << 0 << endl;
    return 0;
  }
  reverse(m.begin(), m.end());
  for (int i = 0; i <= min((int)m.size(), K); i++) {
    int mc = i, pc = K - i;
    if (mc > (int)m.size() || pc > (int)p.size())
      continue;
    int t1 = 1e9, t2 = 1e9;
    if (mc == 0)
      ans = min(ans, p[K - 1]);
    else {
      t1 = -m[mc - 1];
      t2 = -m[mc - 1] * 2;
    }
    if (pc == 0)
      ans = min(ans, -m[K - 1]);
    else {
      t1 += p[pc - 1] * 2;
      t2 += p[pc - 1];
    }
    ans = min({ans, t1, t2});
  }
  cout << ans << endl;

  return 0;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

typedef long long ll;

ll min(ll a, ll b) { return (a < b) ? a : b; }
ll max(ll a, ll b) { return (a > b) ? a : b; }
ll abs(ll a) { return (a < 0) ? -a : a; }

int N, K;
ll x[10001];
int main() {
  scanf("%d %d", &N, &K);

  for (int i = 1; i <= N; i++) {
    scanf("%lld", x + i);
  }

  ll mindist = 10000000000;
  for (int l = 1; l <= N - K + 1; l++) {
    int r = l + K - 1;
    ll distL = abs(x[l] - 0) + x[r] - x[l];
    ll distR = abs(x[r] - 0) + x[r] - x[l];
    ll dist = min(distL, distR);
    // printf("l=%d,r=%d,dist=%lld\n",l,r,dist);
    if (mindist > dist)
      mindist = dist;
  }

  printf("%lld\n", mindist);
}

Fixed submission:
#include <stdio.h>

typedef long long ll;

ll min(ll a, ll b) { return (a < b) ? a : b; }
ll max(ll a, ll b) { return (a > b) ? a : b; }
ll abs(ll a) { return (a < 0) ? -a : a; }

int N, K;
ll x[100001];
int main() {
  scanf("%d %d", &N, &K);

  for (int i = 1; i <= N; i++) {
    scanf("%lld", x + i);
  }

  ll mindist = 10000000000;
  for (int l = 1; l <= N - K + 1; l++) {
    int r = l + K - 1;
    ll distL = abs(x[l] - 0) + x[r] - x[l];
    ll distR = abs(x[r] - 0) + x[r] - x[l];
    ll dist = min(distL, distR);
    // printf("l=%d,r=%d,dist=%lld\n",l,r,dist);
    if (mindist > dist)
      mindist = dist;
  }

  printf("%lld\n", mindist);
}
Problem p03274 (C++)

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

#define int long long

#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

signed main() {
  int N, K;
  cin >> N >> K;

  vint A, B;
  rep(i, N) {
    int a;
    cin >> a;
    if (a >= 0)
      A.pb(a);
    else
      B.pb(-a);
  }
  reverse(all(B));

  int ans = LLONG_MAX;
  rep(t, 2) {
    for (int i = 0; i < K && i < A.size(); i++) {
      int sum = A[i];
      if (i + 1 != K)
        sum += A[i] + B[K - i - 1 - 1];
      chmin(ans, sum);
    }
    swap(A, B);
  }
  cout << ans << endl;
  return 0;
}


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

#define int long long

#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

signed main() {
  int N, K;
  cin >> N >> K;

  vint A, B;
  rep(i, N) {
    int a;
    cin >> a;
    if (a >= 0)
      A.pb(a);
    else
      B.pb(-a);
  }
  reverse(all(B));

  int ans = LLONG_MAX;
  rep(t, 2) {
    for (int i = 0; i < K && i < A.size(); i++) {
      if (K - i - 1 > B.size())
        continue;
      int sum = A[i];
      if (i + 1 != K)
        sum += A[i] + B[K - i - 1 - 1];
      chmin(ans, sum);
    }
    swap(A, B);
  }
  cout << ans << endl;
  return 0;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  long long x[N], tmp, ans = 1e+18;
  for (int i = 0; i < N; i++) {
    cin >> x[i];
  }
  for (int j = 0; j < N - K + 1; j++) {
    tmp = min(abs(x[j]) + abs(x[j + K - 1] - x[j]),
              abs(x[j - K + 1]) + abs(x[j + K - 1] - x[j]));
    ans = min(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  long long x[N], tmp, ans = 1e+18;
  for (int i = 0; i < N; i++) {
    cin >> x[i];
  }
  for (int j = 0; j < N - K + 1; j++) {
    tmp = min(abs(x[j]) + abs(x[j + K - 1] - x[j]),
              abs(x[j + K - 1]) + abs(x[j + K - 1] - x[j]));
    ans = min(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}
Problem p03274 (C++)

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

using namespace std;
typedef long long ll;

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> v;

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

  int idx = 0; // 初めて要素が正に変わるときのインデックス
  while (true) {
    if (v[idx] > 0 || idx == n - 1) {
      break;
    }
    idx++;
  }

  int posi_num = n - idx; // 正の座標の数
  int nega_num = n - posi_num;

  if (posi_num == 0) {
    cout << -v[0] << endl;
    return 0;
  } else if (nega_num == 0) {
    cout << v[k - 1] << endl;
    return 0;
  }

  ll res;
  int left, right;
  if (k <= posi_num) {
    left = idx;
    right = k + idx - 1;
    res = v[right];
  } else {
    left = idx - k + posi_num;
    right = posi_num + idx - 1;
    res = std::min(v[right] + 2 * (-v[left]), 2 * v[right] + (-v[left]));
  }

  while (right > idx || left >= 0) {
    left--;
    right--;

    res = std::min(
        res, std::min(v[right] + 2 * (-v[left]), 2 * v[right] + (-v[left])));
  }

  cout << res << endl;
}

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

using namespace std;
typedef long long ll;

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> v;

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

  ll res = 1000000000;
  for (int i = 0; i <= n - k; i++) {
    ll tmp = std::min(abs(v[i]) + abs(v[i + k - 1] - v[i]),
                      abs(v[i + k - 1]) + abs(v[i] - v[i + k - 1]));
    res = std::min(tmp, res);
  }

  cout << res << endl;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define int ll
int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> pos, neg;
  for (auto i : a) {
    if (i >= 0)
      pos.push_back(i);
    else
      neg.push_back(i);
  }

  sort(neg.rbegin(), neg.rend());
  sort(pos.begin(), pos.end());
  n = neg.size();
  int p = pos.size();

  int ans = 1e18;
  vector<int> left(n + 1), right(p + 1);
  for (int i = 1; i <= n; i++)
    left[i] = -neg[i - 1];
  for (int i = 1; i <= p; i++)
    right[i] = (pos[i - 1]);

  for (int x = 0; x <= k; x++) {
    if (k - x > n)
      continue;
    //		cout<<x<<" "<<k-x<<"\n";
    ans = min(ans, 2 * right[x] + left[k - x]);
    ans = min(ans, 2 * left[k - x] + right[x]);
    //		cout<<ans<<"\n";
  }
  cout << ans;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define int ll
int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> pos, neg;
  for (auto i : a) {
    if (i >= 0)
      pos.push_back(i);
    else
      neg.push_back(i);
  }

  sort(neg.rbegin(), neg.rend());
  sort(pos.begin(), pos.end());
  n = neg.size();
  int p = pos.size();

  int ans = 1e18;
  vector<int> left(n + 1), right(p + 1);
  for (int i = 1; i <= n; i++)
    left[i] = -neg[i - 1];
  for (int i = 1; i <= p; i++)
    right[i] = (pos[i - 1]);

  for (int x = 0; x <= k; x++) {
    if (k - x > n or x > p)
      continue;
    //		cout<<x<<" "<<k-x<<"\n";
    ans = min(ans, 2 * right[x] + left[k - x]);
    ans = min(ans, 2 * left[k - x] + right[x]);
    //		cout<<ans<<"\n";
  }
  cout << ans;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, x, n) for (int i = x; i < n; i++)
using ll = long long;

int N, M;
int X[100], Y[100], h[100];

void gcl(int &x, int &y) {
  if (x < y)
    swap(x, y);
  while (y > 0) {
    int t = x % y;
    x = y;
    y = t;
  }
  return;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> x(N);
  rep(i, 0, N) cin >> x[i];
  int ans = 1000000000;
  rep(i, 0, N) {
    int j = i + K - 1;
    ans = min(ans, x[j] - x[i] + min(abs(x[i]), abs(x[j])));
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, x, n) for (int i = x; i < n; i++)
using ll = long long;

int N, M;
int X[100], Y[100], h[100];

void gcl(int &x, int &y) {
  if (x < y)
    swap(x, y);
  while (y > 0) {
    int t = x % y;
    x = y;
    y = t;
  }
  return;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> x(N);
  rep(i, 0, N) cin >> x[i];
  int ans = 1000000000;
  rep(i, 0, N - K + 1) {
    int j = i + K - 1;
    ans = min(ans, x[j] - x[i] + min(abs(x[i]), abs(x[j])));
  }
  cout << ans << endl;
}
Problem p03274 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
static const int NMAX = 100000;
static const int INF = (1 << 30) - 1;
int N, K, x[NMAX + 1];

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

  int min_time = INF;
  for (int i = 0; i <= N - K; i++) {
    int right = 0, left = 0;
    for (int j = i; j < i + K; j++) {
      left = min(left, x[j]);
      right = max(right, x[j]);
    }
    int rf_time, lf_time;
    if (left != 0)
      rf_time = 2 * right - left;
    else
      rf_time = right;

    if (right != 0)
      lf_time = (-2) * left + right;
    else
      lf_time = -left;

    min_time = min(min_time, rf_time);
    min_time = min(min_time, lf_time);
  }
  cout << min_time << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
static const int NMAX = 100000;
static const int INF = (1 << 30) - 1;
int N, K, x[NMAX + 1];

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

  int min_time = INF;
  for (int i = 0; i <= N - K; i++) {
    int right, left;
    left = min(0, x[i]);
    right = max(0, x[i + K - 1]);

    int rf_time, lf_time;
    if (left != 0)
      rf_time = 2 * right - left;
    else
      rf_time = right;

    if (right != 0)
      lf_time = (-2) * left + right;
    else
      lf_time = -left;

    min_time = min(min_time, rf_time);
    min_time = min(min_time, lf_time);
  }
  cout << min_time << endl;
  return 0;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define mod (1e9 + 7)
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()

typedef long long ll;

void solve1() {
  int n, k;
  cin >> n >> k;
  vector<ll> c(n);
  bool pf = true;
  bool mf = true;
  int prd = 0;
  bool f = true;
  rep(i, n) {
    cin >> c[i];

    if (c[i] >= 0) {
      mf = false;
    } else {
      pf = false;
    }

    if (c[i] >= 0 && f) {
      prd = i;
      f = false;
    }
  }

  ll ans = 0;
  if (pf) {
    ans = c[k - 1];
  } else if (mf) {
    ans = abs(c[n - k]);
  } else {
    ll l = prd;
    ll r = l + k - 1;
    /*cout << l << " " << r << endl;*/
    if (r >= n) {
      ll diff = r - n;
      r -= diff + 1;
      l -= diff + 1;
      /*cout << diff + 1 << endl;*/
    }
    /*cout << l << " " << r << endl;*/
    ans = max(abs(c[r]), abs(c[l])) + 2 * min(abs(c[r]), abs(c[l]));

    while (r >= prd) {
      /*cout << l << " " << r << endl;*/
      ans = min(ans, max(abs(c[r]), abs(c[l])) + 2 * min(abs(c[r]), abs(c[l])));
      /*cout << max(abs(c[r]), abs(c[l])) + 2 * min(abs(c[r]), abs(c[l])) <<
       * endl;*/
      r--;
      l--;
    }
  }

  cout << ans << endl;
}

int main() { solve1(); }

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define mod (1e9 + 7)
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()

typedef long long ll;

void solve1() {
  int n, k;
  cin >> n >> k;
  vector<ll> c(n);
  bool pf = true;
  bool mf = true;
  int prd = 0;
  bool f = true;
  rep(i, n) {
    cin >> c[i];

    if (c[i] >= 0) {
      mf = false;
    } else {
      pf = false;
    }

    if (c[i] >= 0 && f) {
      prd = i;
      f = false;
    }
  }

  ll ans = 0;
  if (pf) {
    ans = c[k - 1];
  } else if (mf) {
    ans = abs(c[n - k]);
  } else {
    ll l = prd;
    ll r = l + k - 1;
    /*cout << l << " " << r << endl;*/
    if (r >= n) {
      ll diff = r - n;
      r -= diff + 1;
      l -= diff + 1;
      /*cout << diff + 1 << endl;*/
    }
    /*cout << l << " " << r << endl;*/
    ans = max(abs(c[r]), abs(c[l])) + 2 * min(abs(c[r]), abs(c[l]));

    while (r >= prd && r >= 0 && l >= 0) {
      /*cout << l << " " << r << endl;*/
      ans = min(ans, max(abs(c[r]), abs(c[l])) + 2 * min(abs(c[r]), abs(c[l])));
      /*cout << max(abs(c[r]), abs(c[l])) + 2 * min(abs(c[r]), abs(c[l])) <<
       * endl;*/
      r--;
      l--;
    }
  }

  cout << ans << endl;
}

int main() { solve1(); }
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\n"
#define chmax(a, b)                                                            \
  {                                                                            \
    if (a < b)                                                                 \
      a = b;                                                                   \
  }
#define chmin(a, b)                                                            \
  {                                                                            \
    if (a > b)                                                                 \
      a = b;                                                                   \
  }
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
static const ll INF = 1e18;
static const ll MAX = 1e5 + 7;
static ll MOD = 1e9 + 7;
ll max(ll a, ll b) { return a > b ? a : b; }
ll min(ll a, ll b) { return a < b ? a : b; }

int main(void) {
  ll N, K;
  cin >> N >> K;
  ll x[MAX];
  ll i;

  for (i = 0; i < N; i++) {
    cin >> x[i];
  }

  ll ans = INF;
  for (i = 0; i < N; i++) {

    if (x[i] > 0) {
      chmin(ans, x[i + K - 1]);
    } else if (x[i + K - 1] < 0) {
      chmin(ans, abs(x[i]));
    } else {
      chmin(ans, abs(x[i]) * 2 + x[i + K - 1]);
      chmin(ans, abs(x[i]) + x[i + K - 1] * 2);
    }
  }

  pt(ans);
}


Fixed submission:
#include <bits/stdc++.h>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\n"
#define chmax(a, b)                                                            \
  {                                                                            \
    if (a < b)                                                                 \
      a = b;                                                                   \
  }
#define chmin(a, b)                                                            \
  {                                                                            \
    if (a > b)                                                                 \
      a = b;                                                                   \
  }
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
static const ll INF = 1e18;
static const ll MAX = 1e5 + 7;
static ll MOD = 1e9 + 7;
ll max(ll a, ll b) { return a > b ? a : b; }
ll min(ll a, ll b) { return a < b ? a : b; }

int main(void) {
  ll N, K;
  cin >> N >> K;
  ll x[MAX];
  ll i;

  for (i = 0; i < N; i++) {
    cin >> x[i];
  }

  ll ans = INF;
  for (i = 0; i < N - K + 1; i++) {

    if (x[i] > 0) {
      chmin(ans, x[i + K - 1]);
    } else if (x[i + K - 1] < 0) {
      chmin(ans, abs(x[i]));
    } else {
      chmin(ans, abs(x[i]) * 2 + x[i + K - 1]);
      chmin(ans, abs(x[i]) + x[i + K - 1] * 2);
    }
  }

  pt(ans);
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

int n, k;
long long x[10005];

long long calc(int st) {
  int ed = st + k - 1;

  return min(abs(x[st]), abs(x[ed])) + abs(x[ed] - x[st]);
}

int main() {

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

  long long ans = 5 * 100000000;
  for (int i = 0; i < n - k + 1; i++) {
    ans = min(ans, calc(i));
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

int n, k;
long long x[100005];

long long calc(int st) {
  int ed = st + k - 1;

  return min(abs(x[st]), abs(x[ed])) + abs(x[ed] - x[st]);
}

int main() {

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

  long long ans = 5 * 100000000;
  for (int i = 0; i < n - k + 1; i++) {
    ans = min(ans, calc(i));
  }
  cout << ans << endl;

  return 0;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, k;
int arr[100100], pos;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  pos = n;
  for (int i = 0; i < n; i++)
    if (arr[i] >= 0) {
      pos = i;
      break;
    }
  int Ans = INT_MAX;
  if (n - pos >= k)
    Ans = min(Ans, arr[pos + k - 1]);
  if (pos >= k)
    Ans = min(Ans, -arr[pos - k]);
  for (int i = max(1, pos - k + 1); i <= min(k - 1, pos); i++)
    Ans = min(Ans, -arr[pos - i] + arr[pos - i + k - 1] +
                       min(-arr[pos - i], arr[pos - i + k - 1]));
  cout << Ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, k;
int arr[100100], pos;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  pos = n;
  for (int i = 0; i < n; i++)
    if (arr[i] >= 0) {
      pos = i;
      break;
    }
  int Ans = INT_MAX;
  if (n - pos >= k)
    Ans = min(Ans, arr[pos + k - 1]);
  if (pos >= k)
    Ans = min(Ans, -arr[pos - k]);
  for (int i = max(1, k - (n - pos)); i <= min(k - 1, pos); i++)
    Ans = min(Ans, -arr[pos - i] + arr[pos + k - i - 1] +
                       min(-arr[pos - i], arr[pos + k - i - 1]));
  cout << Ans << endl;
  return 0;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
const int maxn = 10005;
int a[maxn], k, n;
int Min = 2147483647;
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n - k + 1; i++) {
    int l = i, r = i + k - 1;
    Min = min(Min, a[r] - a[l] + min(abs(a[l]), abs(a[r])));
  }
  printf("%d", Min);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
const int maxn = 100005;
int a[maxn], k, n;
int Min = 2147483647;
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n - k + 1; i++) {
    int l = i, r = i + k - 1;
    Min = min(Min, a[r] - a[l] + min(abs(a[l]), abs(a[r])));
  }
  printf("%d", Min);
  return 0;
}
Problem p03274 (C++)

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> p;
  vector<int> m;
  rep(i, N) {
    int x;
    cin >> x;
    if (x < 0) {
      m.push_back(-x);
    } else {
      p.push_back(x);
    }
  }

  int ans = 1001001001;
  int psize = p.size();
  int msize = m.size();
  for (int i = 0; i <= msize; i++) {
    int pnum = K - i;
    if (pnum > psize)
      continue;
    int left, right;
    if (i == 0) {
      left = 0;
    } else {
      left = m[msize - i];
    }
    if (pnum == 0) {
      right = 0;
    } else {
      right = p[pnum - 1];
    }
    int now = min(left * 2 + right, left + right * 2);
    ans = min(ans, now);
  }

  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;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> p;
  vector<int> m;
  rep(i, N) {
    int x;
    cin >> x;
    if (x < 0) {
      m.push_back(-x);
    } else {
      p.push_back(x);
    }
  }

  int ans = 1001001001;
  int psize = p.size();
  int msize = m.size();
  for (int i = 0; i <= msize; i++) {
    int pnum = K - i;
    if (pnum > psize || pnum < 0)
      continue;
    int left, right;
    if (i == 0) {
      left = 0;
    } else {
      left = m[msize - i];
    }
    if (pnum == 0) {
      right = 0;
    } else {
      right = p[pnum - 1];
    }
    int now = min(left * 2 + right, left + right * 2);
    ans = min(ans, now);
  }

  cout << ans << endl;

  return 0;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>

#define rep(i, a) for (int i = 0; i < a; i++)
#define llint long long int
using namespace std;

const int MAX_x = 100005;
const llint INF = 999999999999999;

int main() {
  int n, k;
  llint x[MAX_x];
  cin >> n >> k;

  int bgn, end;
  bool pn = false;
  rep(i, n) {
    cin >> x[i];
    if (!pn && x[i] >= 0) {
      bgn = i;
      if (bgn + k <= n)
        end = bgn + k - 1;
      else {
        bgn = n - k;
        end = n - 1;
      }
      pn = true;
    }
  }

  llint dst, mn = INF;
  do {
    dst = min(abs(x[bgn]) + abs(x[bgn] - x[end]),
              abs(x[end]) + abs(x[bgn] - x[end]));
    if (dst < mn)
      mn = dst;
    // printf("%lld %d %d\n", dst, bgn, end);

    --bgn;
    --end;
  } while (bgn >= 0);

  cout << mn << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>

#define rep(i, a) for (int i = 0; i < a; i++)
#define llint long long int
using namespace std;

const int MAX_x = 100005;
const llint INF = 999999999999999;

int main() {
  int n, k;
  llint x[MAX_x];
  cin >> n >> k;

  int bgn, end;
  bool pn = false;
  rep(i, n) {
    cin >> x[i];
    if ((!pn && x[i] >= 0) || (i == n - 1 && x[i] < 0)) {
      bgn = i;
      if (bgn + k <= n)
        end = bgn + k - 1;
      else {
        bgn = n - k;
        end = n - 1;
      }
      pn = true;
    }
  }

  llint dst, mn = INF;
  do {
    dst = min(abs(x[bgn]) + abs(x[bgn] - x[end]),
              abs(x[end]) + abs(x[bgn] - x[end]));
    if (dst < mn)
      mn = dst;
    // printf("%lld %d %d\n", dst, bgn, end);

    --bgn;
    --end;
  } while (bgn >= 0);

  cout << mn << endl;
}
Problem p03274 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int INF = 1e9;

int main() {
  int n, k, ans = INF;
  cin >> n >> k;
  vector<int> x(n);
  rep(i, n) cin >> x[i];
  for (int i = 0; i < n - k + 1; ++i)
    ans = min({abs(x[i]) + abs(x[i - k - 1] - x[i]),
               abs(x[i - k - 1]) + abs(x[i - k - 1] - x[i]), ans});

  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;
typedef long long ll;
const int INF = 1e9;

int main() {
  int n, k, ans = INF;
  cin >> n >> k;
  vector<int> x(n);
  rep(i, n) cin >> x[i];
  for (int i = 0; i < n - k + 1; ++i)
    ans = min({abs(x[i]) + abs(x[i + k - 1] - x[i]),
               abs(x[i + k - 1]) + abs(x[i + k - 1] - x[i]), ans});

  cout << ans << endl;
  return 0;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
using namespace std;
using ll = long long;

int main() {
  ll N, K;
  cin >> N >> K;

  ll X[N];
  ll zero = -1;
  ll maxIdx = 0;
  for (ll i = 0; i < N; i++) {
    cin >> X[i];
    if (zero == -1 && X[i] >= 0)
      zero = i;
    maxIdx = i;
  }

  if (zero == -1) {
    cout << abs(X[maxIdx - K + 1]);
    return 0;
  }
  if (zero == 0) {
    cout << abs(X[K - 1]);
    return 0;
  }

  ll left = 0, right = 0;
  if (zero <= K) {
    left = 0;
  } else if (zero > K) {
    left = zero - K;
  }
  right = zero + K - 1;

  // cout << "zero: " << zero << " left: " << left << " right: " << right <<
  // endl;

  ll ans = 1000000000;
  for (ll i = 0; i < (right - left + 1 - K + 1); i++) {
    ll sum1 = 0;
    ll sum2 = 0;
    sum1 += abs(X[left + i]) * 2 + X[left + i + K - 1];
    sum2 += abs(X[left + i]) + X[left + i + K - 1] * 2;
    ll sum = min(sum1, sum2);
    ans = min(ans, sum);
  }

  cout << ans;

  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
using ll = long long;

int main() {
  ll N, K;
  cin >> N >> K;

  ll X[N];
  ll zero = -1;
  ll maxIdx = 0;
  for (ll i = 0; i < N; i++) {
    cin >> X[i];
    if (zero == -1 && X[i] >= 0)
      zero = i;
    maxIdx = i;
  }

  if (zero == -1) {
    cout << abs(X[maxIdx - K + 1]);
    return 0;
  }
  if (zero == 0) {
    cout << abs(X[K - 1]);
    return 0;
  }

  ll left = 0, right = 0;
  if (zero <= K) {
    left = 0;
  } else if (zero > K) {
    left = zero - K;
  }
  right = zero + K - 1;

  // cout << "zero: " << zero << " left: " << left << " right: " << right <<
  // endl;

  ll ans = 1000000000;
  for (ll i = 0; i < (right - left + 1 - K + 1); i++) {
    ll sum1 = 0;
    ll sum2 = 0;
    if (left + i + K - 1 > maxIdx)
      break;
    sum1 += abs(X[left + i]) * 2 + X[left + i + K - 1];
    sum2 += abs(X[left + i]) + X[left + i + K - 1] * 2;
    ll sum = min(sum1, sum2);
    ans = min(ans, sum);
  }

  cout << ans;

  return 0;
}
Problem p03274 (C++)

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

using ll = long long;
using ull = unsigned long long;

const double PI = acos(-1);
const int inf = 2e9;
const ll INF = 2e18;
const ll MOD = 1e9 + 7;

#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(a) begin(a), end(a)

int main(void) {
  int N, K;
  cin >> N >> K;
  vector<int> X;
  REP(i, N) {
    int t;
    cin >> t;
    if (t == 0)
      K--;
    else
      X.pb(t);
  }
  if (X.size() == 0) {
    cout << 0 << endl;
    return 0;
  }
  N = X.size();
  int zero = 0;
  if (X[0] >= 0)
    zero = 0;
  else if (X[N - 1] < 0)
    zero = N;
  else {
    for (int i = 1; i < N; i++) {
      if (X[i - 1] < 0 && X[i] >= 0)
        zero = i;
    }
  }
  ll ans = inf;
  REP(i, N) {
    ll temp;
    if (i < zero) {
      int g = zero - i;
      int d = K - g;
      if (g > K)
        continue;
      if (g == K)
        temp = abs(X[i]);
      else
        temp = abs(X[i]) * 2 + X[zero + d - 1];
    } else {
      int g = i - zero + 1;
      int d = K - g;
      if (g > K)
        continue;
      if (g == K)
        temp = abs(X[i]);
      else
        temp = abs(X[i]) * 2 + abs(X[i - K + 1]);
    }
    ans = min(temp, ans);
  }
  cout << ans << endl;
  return 0;
}

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

using ll = long long;
using ull = unsigned long long;

const double PI = acos(-1);
const int inf = 2e9;
const ll INF = 2e18;
const ll MOD = 1e9 + 7;

#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(a) begin(a), end(a)

int main(void) {
  int N, K;
  cin >> N >> K;
  vector<int> X(N);
  REP(i, N) cin >> X[i];
  ll ans = INF;
  REP(i, N - K + 1) {
    int l = i;
    int r = i + K - 1;
    ll temp1 = abs(X[l]) + abs(X[r] - X[l]);
    ll temp2 = abs(X[r]) + abs(X[r] - X[l]);
    ans = min(ans, min(temp1, temp2));
  }
  cout << ans << endl;
  return 0;
}
Problem p03274 (C++)

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

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


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

int main() {
  long long int n, k, ans = INT_MAX;
  cin >> n >> k;
  long long int x[n];
  for (int i = 0; i < n; i++)
    cin >> x[i];
  for (int i = 0; i < n - k + 1; i++) {
    ans = min(ans, abs(x[i]) + abs(x[i + k - 1] - x[i]));
    ans = min(ans, abs(x[k + i - 1]) + abs(x[i + k - 1] - x[i]));
  }
  cout << ans << endl;
}
Problem p03274 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define all(x) (x).begin(), (x).end()
#define print(x) cout << (x) << endl;

typedef long long ll;
const ll INF = 99999999999999;
const ll MOD = 1000000007;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll number1, ll number2) {
  return number1 * number2 / gcd(number1, number2);
}
template <class T> bool contain(const std::string &s, const T &v) {
  // contain(to_string(N), '3')
  return s.find(v) != std::string::npos;
}

int ctoi(char c) {
  if (c >= '0' && c <= '9')
    return c - '0';
  return 0;
}
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;
  vector<ll> x(n);
  rep(i, n) { cin >> x[i]; }
  ll ans = INF;
  rep(i, n - k + 1) {
    ll tmp = 0;
    ll a = *max_element(x.begin() + i, x.begin() + i + k);
    ll b = *min_element(x.begin() + i, x.begin() + i + k);

    tmp = a - b + min(abs(a), abs(b));
    ans = min(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define all(x) (x).begin(), (x).end()
#define print(x) cout << (x) << endl;

typedef long long ll;
const ll INF = 99999999999999;
const ll MOD = 1000000007;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll number1, ll number2) {
  return number1 * number2 / gcd(number1, number2);
}
template <class T> bool contain(const std::string &s, const T &v) {
  // contain(to_string(N), '3')
  return s.find(v) != std::string::npos;
}

int ctoi(char c) {
  if (c >= '0' && c <= '9')
    return c - '0';
  return 0;
}
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;
  vector<ll> x(n);
  rep(i, n) { cin >> x[i]; }
  ll ans = INF;
  rep(i, n - k + 1) {
    ll tmp = 0;
    ll a = x[i + k - 1];
    ll b = x[i];

    tmp = a - b + min(abs(a), abs(b));
    ans = min(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll N, K;
  cin >> N >> K;
  ll x[N + 1];
  for (ll i = 0; i < N; i++) {
    cin >> x[i];
  }
  ll cnt1 = 0, cnt2 = 0;
  for (ll i = 0; i < N; i++) {
    if (x[i] >= 0)
      cnt1++;
    if (x[i] <= 0)
      cnt2++;
  }
  if (cnt1 == N)
    cout << x[K - 1] << endl;
  else if (cnt2 == N)
    cout << abs(x[N - K]) << endl;
  else {
    ll y = 10000000000000000;
    for (ll i = 0; i < N; i++) {
      if (x[i + K - 1] >= 0 && x[i] <= 0) {
        ll a = 2 * x[i + K - 1] + abs(x[i]);
        ll b = x[i + K - 1] + 2 * abs(x[i]);
        y = min(min(a, b), y);
      }
    }
    cout << y << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll N, K;
  cin >> N >> K;
  ll x[N + 1];
  for (ll i = 0; i < N; i++) {
    cin >> x[i];
  }
  ll cnt1 = 0, cnt2 = 0;
  for (ll i = 0; i < N; i++) {
    if (x[i] >= 0)
      cnt1++;
    if (x[i] <= 0)
      cnt2++;
  }
  if (cnt1 == N)
    cout << x[K - 1] << endl;
  else if (cnt2 == N)
    cout << abs(x[N - K]) << endl;
  else {
    ll y = 10000000000000000;
    for (ll i = 0; i < N - K + 1; i++) {
      if (x[i + K - 1] >= 0 && x[i] <= 0) {
        ll a = 2 * x[i + K - 1] + abs(x[i]);
        ll b = x[i + K - 1] + 2 * abs(x[i]);
        y = min(min(a, b), y);
      }
    }
    cout << y << endl;
  }
}
Problem p03274 (C++)

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

typedef long long ll;

ll N, K;
ll x[100000];

int main() {
  scanf("%lld%lld", &N, &K);
  ll sum = 0;
  ll minl = 1e9;
  for (long i = 0; i < N; i++) {
    scanf("%lld", &x[i]);
  }
  sum = x[K - 1] - x[0];
  minl = min(minl, min(sum + abs(x[0]), sum + abs(x[K - 1])));
  for (long i = K; i < N; i++) {
    sum = sum + abs(x[i] - x[i - 1]) - abs(x[i - K] - x[i + K + 1]);
    minl = min(minl, min(sum + abs(x[i]), sum + abs(x[i - K])));
  }
  printf("%lld", minl);
}


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

typedef long long ll;

ll N, K;
ll x[100000];

int main() {
  scanf("%lld%lld", &N, &K);
  ll sum = 0;
  ll minl = 1e9;
  for (long i = 0; i < N; i++) {
    scanf("%lld", &x[i]);
  }
  for (long i = K - 1; i < N; i++) {
    sum = abs(x[i] - x[i - K + 1]);
    minl = min(minl, min(sum + abs(x[i]), sum + abs(x[i - K + 1])));
  }
  printf("%lld", minl);
}
Problem p03274 (C++)

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

int main() {
  int N, K;
  cin >> N >> K;

  int x[N];
  vector<int> negative;
  vector<int> positive;
  for (int i = 0; i < N; i++) {
    cin >> x[i];
    if (x[i] < 0) {
      negative.push_back(-x[i]);
    } else if (0 < x[i]) {
      positive.push_back(x[i]);
    } else {
      K--;
      if (K == 0) {
        cout << 0 << endl;
        return 0;
      }
    }
  }
  reverse(negative.begin(), negative.end());
  positive.insert(positive.begin(), 0);
  negative.insert(negative.begin(), 0);

  int ans = INT_MAX;
  for (int i = 0; i < negative.size(); i++) {
    if (i + positive.size() - 1 >= K) {
      ans = min(ans, 2 * negative[i] + positive[K - i]);
      ans = min(ans, negative[i] + 2 * positive[K - i]);
    }
  }
  cout << ans << endl;
}


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

int main() {
  int N, K;
  cin >> N >> K;

  int x[N];
  vector<int> negative;
  vector<int> positive;
  for (int i = 0; i < N; i++) {
    cin >> x[i];
    if (x[i] < 0) {
      negative.push_back(-x[i]);
    } else if (0 < x[i]) {
      positive.push_back(x[i]);
    } else {
      K--;
      if (K == 0) {
        cout << 0 << endl;
        return 0;
      }
    }
  }
  reverse(negative.begin(), negative.end());
  positive.insert(positive.begin(), 0);
  negative.insert(negative.begin(), 0);

  int ans = INT_MAX;
  for (int i = 0; i < negative.size(); i++) {
    if (i > K) {
      break;
    }
    if (i + positive.size() - 1 >= K) {
      ans = min(ans, 2 * negative[i] + positive[K - i]);
      ans = min(ans, negative[i] + 2 * positive[K - i]);
    }
  }
  cout << ans << endl;
}
Problem p03274 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int N, K;
  int x[100000];
  cin >> N >> K;
  int len1 = 0;
  int len2 = 0;
  int m = 2000000001;
  for (int i = 0; i < N; i++) {
    cin >> x[i];
  }
  for (int i = 0; i < N - K + 1; i++) {
    for (int j = 0; j < K; j++) {
      len1 = abs(x[i]) + x[i + K - 1] - x[i];
      len2 = abs(x[i + K - 1]) + x[i + K - 1] - x[i];
      m = min(m, len1);
      m = min(m, len2);
    }
  }
  cout << m << '\n';
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int N, K;
  int x[100000];
  cin >> N >> K;
  int len1 = 0;
  int len2 = 0;
  int m = 2000000001;
  for (int i = 0; i < N; i++) {
    cin >> x[i];
  }
  for (int i = 0; i < N - K + 1; i++) {
    // for(int j=0;j<K;j++){
    len1 = abs(x[i]) + x[i + K - 1] - x[i];
    len2 = abs(x[i + K - 1]) + x[i + K - 1] - x[i];
    m = min(m, len1);
    m = min(m, len2);
    //}
  }
  cout << m << '\n';
}
Problem p03274 (Python)

Buggy submission (status: Time Limit Exceeded):
def solve():
    n, k = map(int, input().split())
    xs = list(map(int, input().split()))
    min_dist = 10**9
    for i in range(n - k + 1):
        part = xs[i : i + k]
        length = abs(part[0] - part[-1])
        dist = min(abs(part[0]) + length, abs(part[-1]) + length)
        if dist < min_dist:
            min_dist = dist
    return min_dist


if __name__ == "__main__":
    print(solve())


Fixed submission:
def solve():
    n, k = map(int, input().split())
    xs = list(map(int, input().split()))
    min_dist = 10**9
    for i in range(n - k + 1):
        length = abs(xs[i] - xs[i + k - 1])
        dist = min(abs(xs[i]) + length, abs(xs[i + k - 1]) + length)
        if dist < min_dist:
            min_dist = dist
    return min_dist


if __name__ == "__main__":
    print(solve())
Problem p03274 (C++)

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

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define repp(i, m, n) for (int i = m, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define clr(ar, val) memset(ar, val, sizeof(ar))
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef long double ld;
const ll INF = 1000000000000LL;

int main(void) {
  int n, k;
  ll ans = INF;
  vector<ll> a[2];
  cin >> n >> k;
  rep(i, n) {
    int t;
    cin >> t;
    if (t >= 0)
      a[1].push_back(t);
    else
      a[0].push_back(-1 * t);
  }
  sort(all(a[0]));
  sort(all(a[1]));
  rep(i, k + 1) {
    int j = k - i;
    if (i == 0 && a[1].size() >= k)
      chmin(ans, a[1][k - 1]);
    else if (j == 0 && a[0].size() >= k)
      chmin(ans, a[0][k - 1]);
    else if (a[0].size() >= i && a[1].size() >= j) {
      chmin(ans, 2 * a[1][i - 1] + a[0][j - 1]);
      chmin(ans, 2 * a[0][i - 1] + a[1][j - 1]);
    }
    // cout << ans << " ";
  }
  cout << ans << endl;
  return 0;
}

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

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define repp(i, m, n) for (int i = m, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define clr(ar, val) memset(ar, val, sizeof(ar))
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef long double ld;
const ll INF = 1000000000000LL;

int main(void) {
  int n, k;
  ll ans = INF;
  vector<ll> a[2];
  cin >> n >> k;
  rep(i, n) {
    int t;
    cin >> t;
    if (t >= 0)
      a[1].push_back(t);
    else
      a[0].push_back(-1 * t);
  }
  sort(all(a[0]));
  sort(all(a[1]));
  rep(i, k + 1) {
    int j = k - i;
    if (i == 0 && a[1].size() >= k)
      chmin(ans, a[1][k - 1]);
    else if (j == 0 && a[0].size() >= k)
      chmin(ans, a[0][k - 1]);
    else if (a[0].size() >= i && a[1].size() >= j) {
      chmin(ans, 2 * a[1][j - 1] + a[0][i - 1]);
      chmin(ans, 2 * a[0][i - 1] + a[1][j - 1]);
    }
    // cout << ans << " ";
  }
  cout << ans << endl;
  return 0;
}
Problem p03274 (Python)

Buggy submission (status: Runtime Error):
def main():
    n, k, *x = map(int, open(0).read().split())
    ans = min(
        [x[i + k - 1] - x[i] + min(abs(x[i + k - 1]), abs(x[i])) for i in range(n - k)]
    )
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    n, k, *x = map(int, open(0).read().split())
    l = x[k - 1] - x[0] + min(abs(x[k - 1]), abs(x[0]))

    for i, j in zip(x, x[k - 1 :]):
        y = j - i + min(abs(i), abs(j))
        if y - l < 0:
            l = y

    print(l)


if __name__ == "__main__":
    main()