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

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

int main() {

  int line;
  int row;
  cin >> line;
  cin >> row;

  vector<vector<char>> masu(line + 1, vector<char>(row + 1));

  for (int i = 0; i < line; i++) {
    for (int j = 0; j < row; j++) {
      cin >> masu.at(i).at(j);
    }
  }

  // for (int i = 0; i < line; i++) {
  //  for (int j = 0; j < row; j++) {
  //    cout << masu.at(i).at(j);
  //  }
  //  cout <<endl;
  // }
  // cout << "----" << endl;
  int line_flg;
  int flg;

  int line_out_flg;
  for (int i = 0; i < line; i++) { // i --> line
    line_out_flg = 0;
    if (masu.at(i).at(0) != '#') {
      for (int j = 1; j < row; j++) {
        if (masu.at(i).at(j) == '#') {
          line_out_flg = 1;
          // cout << "line = " << i << ", row = " << j << "find #" << endl;
          break;
        }
      }
    } else {
      line_out_flg = 1;
    }
    if (line_out_flg == 0) {
      // cout << i << " skip " << endl;
      continue;
    }
    line_flg = 0;
    for (int j = 0; j < row; j++) { // j --> row
      flg = 0;
      if (masu.at(i).at(j) != '#') {
        for (int k = 0; k < row; k++) {
          if (k != i) {
            if (masu.at(k).at(j) == '#') {
              flg = 1;
              break;
            }
          }
        }
      } else {
        flg = 1;
      }
      // cout << "line = " << i << ", row = " << j << ", flg = " << flg << endl;
      if (flg == 1) {
        cout << masu.at(i).at(j);
        line_flg = 1;
      }
    }
    if (line_flg == 1) {
      cout << endl;
    }
  }
}


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

int main() {

  int line;
  int row;
  cin >> line;
  cin >> row;

  vector<vector<char>> masu(line + 1, vector<char>(row + 1));

  for (int i = 0; i < line; i++) {
    for (int j = 0; j < row; j++) {
      cin >> masu.at(i).at(j);
    }
  }

  // for (int i = 0; i < line; i++) {
  //  for (int j = 0; j < row; j++) {
  //    cout << masu.at(i).at(j);
  //  }
  //  cout <<endl;
  // }
  // cout << "----" << endl;
  int line_flg;
  int flg;

  int line_out_flg;
  for (int i = 0; i < line; i++) { // i --> line
    line_out_flg = 0;
    if (masu.at(i).at(0) != '#') {
      for (int j = 1; j < row; j++) {
        if (masu.at(i).at(j) == '#') {
          line_out_flg = 1;
          // cout << "line = " << i << ", row = " << j << "find #" << endl;
          break;
        }
      }
    } else {
      line_out_flg = 1;
    }
    if (line_out_flg == 0) {
      // cout << i << " skip " << endl;
      continue;
    }
    line_flg = 0;
    for (int j = 0; j < row; j++) { // j --> row
      flg = 0;
      if (masu.at(i).at(j) != '#') {
        for (int k = 0; k < line; k++) {
          if (k != i) {
            if (masu.at(k).at(j) == '#') {
              flg = 1;
              break;
            }
          }
        }
      } else {
        flg = 1;
      }
      // cout << "line = " << i << ", row = " << j << ", flg = " << flg << endl;
      if (flg == 1) {
        cout << masu.at(i).at(j);
        line_flg = 1;
      }
    }
    if (line_flg == 1) {
      cout << endl;
    }
  }
}
Problem p03273 (C++)

Buggy submission (status: Runtime Error):

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main() {
  char a[10][10] = {0};
  int h = 0, w = 0, i = 0, j = 0, num[10][10] = {0}, c = 0, x = 0;
  bool flag;
  cin >> h >> w;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      cin >> a[i][j];
    }
  }

  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (a[i][j] != '.') {
        c = 1;
      }
    }
    if (c == 1) {
      c = 0;
      continue;
    }
    for (j = 0; j < w; j++) {
      num[i][j] = 1;
    }
  }

  for (i = 0; i < w; i++) {

    for (j = 0; j < h; j++) {
      if (a[j][i] != '.') {
        c = 1;
      }
    }
    if (c == 1) {
      c = 0;
      continue;
    }
    for (j = 0; j < h; j++) {
      num[j][i] = 1;
    }
  }

  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (num[i][j] == 1) {
        cout << "";
        x++;
        continue;
      }
      cout << a[i][j];
    }
    if (x == w) {
      x = 0;
      continue;
    }
    x = 0;
    cout << endl;
  }

  return 0;
}


Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main() {
  char a[110][110] = {0};
  int h = 0, w = 0, i = 0, j = 0, num[110][110] = {0}, c = 0, x = 0;
  bool flag;
  cin >> h >> w;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      cin >> a[i][j];
    }
  }

  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (a[i][j] != '.') {
        c = 1;
      }
    }
    if (c == 1) {
      c = 0;
      continue;
    }
    for (j = 0; j < w; j++) {
      num[i][j] = 1;
    }
  }

  for (i = 0; i < w; i++) {

    for (j = 0; j < h; j++) {
      if (a[j][i] != '.') {
        c = 1;
      }
    }
    if (c == 1) {
      c = 0;
      continue;
    }
    for (j = 0; j < h; j++) {
      num[j][i] = 1;
    }
  }

  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (num[i][j] == 1) {
        cout << "";
        x++;
        continue;
      }
      cout << a[i][j];
    }
    if (x == w) {
      x = 0;
      continue;
    }
    x = 0;
    cout << endl;
  }

  return 0;
}
Problem p03273 (C++)

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

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<char>> vec(h, vector<char>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> vec.at(i).at(j);
    }
  }
  vector<bool> row(h, 0);
  vector<bool> col(w, 0);

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (vec.at(i).at(j) == '#') {
        row.at(i) = 1;
        break;
      }
    }
  }

  for (int j = 0; j < w; j++) {
    for (int i = 0; i < h; i++) {
      if (vec.at(j).at(i) == '#') {
        col.at(j) = 1;
        break;
      }
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (row.at(i) and col.at(j)) {
        cout << vec.at(i).at(j);
      }
    }
    if (row.at(i)) {
      cout << endl;
    }
  }
}

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

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<char>> vec(h, vector<char>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> vec.at(i).at(j);
    }
  }
  vector<bool> row(h, 0);
  vector<bool> col(w, 0);

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (vec.at(i).at(j) == '#') {
        row.at(i) = 1;
        break;
      }
    }
  }

  for (int j = 0; j < w; j++) {
    for (int i = 0; i < h; i++) {
      if (vec.at(i).at(j) == '#') {
        col.at(j) = 1;
        break;
      }
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (row.at(i) and col.at(j)) {
        cout << vec.at(i).at(j);
      }
    }
    if (row.at(i)) {
      cout << endl;
    }
  }
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):

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

// #define int long long

signed main(void) {
  int num; // num
  // num = 0; // count, result
  // num = numeric_limits<int>::min(); // max
  num = numeric_limits<int>::max(); // min
  int N;
  int M;
  array<int, 10000> A;
  // string S;
  // string T;

  cin >> N >> M;
  for (auto it = A.begin(); it < A.begin() + N; ++it)
    cin >> *it;

  for (int i = 0; i < N - M + 1; ++i) {
    int tmp = min(abs(A[i]) + abs(A[i] - A[i + M - 1]),
                  abs(A[i + M - 1]) + abs(A[i] - A[i + M - 1]));
    num = min(num, tmp);
  }
  cout << num << endl;

  // cin >> N;
  // for (auto it = A.begin(); it < A.begin() + N; ++it)
  //   cin >> *it;
  // for (auto it = A.begin(); it < A.begin() + N; ++it)
  //   if (*it % 2 == 0) ++num;
  // cout << num << endl;

  return 0;
}


Fixed submission:

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

// #define int long long

signed main(void) {
  int num; // num
  // num = 0; // count, result
  // num = numeric_limits<int>::min(); // max
  num = numeric_limits<int>::max(); // min
  int N;
  int M;
  array<int, 1000000> A;
  // string S;
  // string T;

  cin >> N >> M;
  for (auto it = A.begin(); it < A.begin() + N; ++it)
    cin >> *it;

  for (int i = 0; i < N - M + 1; ++i) {
    int tmp = min(abs(A[i]) + abs(A[i] - A[i + M - 1]),
                  abs(A[i + M - 1]) + abs(A[i] - A[i + M - 1]));
    num = min(num, tmp);
  }
  cout << num << endl;

  // cin >> N;
  // for (auto it = A.begin(); it < A.begin() + N; ++it)
  //   cin >> *it;
  // for (auto it = A.begin(); it < A.begin() + N; ++it)
  //   if (*it % 2 == 0) ++num;
  // cout << num << endl;

  return 0;
}
Problem p03274 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <vector>

typedef long long ll;

using namespace std;

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

  int d = INT_MAX;
  for (int i = 0; i < N - K + 1; i++) {
    int vmax = INT_MIN;
    int vmin = INT_MAX;
    for (int j = i; j < i + K; j++) {
      vmax = max(vmax, x[j]);
      vmin = min(vmin, x[j]);
    }
    int cr = abs(vmax) + abs(vmax - vmin);
    int cl = abs(vmin) + abs(vmax - vmin);
    d = min(d, min(cr, cl));
  }
  cout << d << endl;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <vector>

typedef long long ll;

using namespace std;

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

  int d = INT_MAX;
  for (int i = 0; i < N - K + 1; i++) {
    int l = i, r = i + K - 1;
    int cr = abs(x[r]) + abs(x[r] - x[l]);
    int cl = abs(x[l]) + abs(x[r] - x[l]);
    d = min(d, min(cr, cl));
  }
  cout << d << endl;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
typedef long long ll;
int main(void) {
  // Your code here!
  ll n, k;
  cin >> n >> k;
  int s[n];
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  ll ans = s[k - 1] - s[0] + min(abs(s[0]), abs(s[k - 1]));
  for (int i = 0; i < k; i++) {
    ll p = s[i + k - 1] - s[i] + min(abs(s[i + k - 1]), abs(s[i]));
    ans = min(ans, p);
  }
  cout << ans << endl;
}


Fixed submission:
#include <iostream>
using namespace std;
typedef long long ll;
int main(void) {
  // Your code here!
  ll n, k;
  cin >> n >> k;
  int s[n];
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  ll ans = s[k - 1] - s[0] + min(abs(s[0]), abs(s[k - 1]));
  for (int i = 0; i < n - k + 1; i++) {
    ll p = s[i + k - 1] - s[i] + min(abs(s[i + k - 1]), abs(s[i]));
    ans = min(ans, p);
  }
  cout << ans << endl;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
const int MOD = (int)1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> c(n);
  rep(i, n) cin >> c[i];
  int index = distance(c.begin(), lower_bound(all(c), 0));
  // cout << index << endl;
  int count = (index < n && c[index] == 0) ? 1 : 0;
  int left = k - count;
  int ans = (index + k - 1 < n ? c[index + k - 1] : MOD);
  rep(i, min(index - 1, left)) {
    int t = index - i - 1;
    if (i == left) {
      ans = min(ans, abs(c[t]));
      continue;
    }
    int buf = left - i - 1;
    ans = min(ans, -2 * c[t] + c[index + buf - 1]);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
const int MOD = (int)1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int ans = MOD;
  rep(i, n - k + 1) {
    int l = a[i];
    int r = a[i + k - 1];
    ans = min(abs(r) + abs(r - l), ans);
    ans = min(abs(l) + abs(r - l), ans);
  }
  cout << ans << endl;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  int rousoku[10001] = {};

  for (int i = 0; i < n; i++) {
    cin >> rousoku[i];
  }

  long long ans = 1e10;

  // 連続したろうそくを選択する方法がベスト
  for (int i = 0; i < n - k + 1; i++) {
    // 右から辿る方と左から辿る方
    long long l, r = 0;
    l = abs(rousoku[i]) + abs(rousoku[i] - rousoku[i + k - 1]);
    r = abs(rousoku[i + k - 1]) + abs(rousoku[i] - rousoku[i + k - 1]);
    long long temp = min(l, r);
    ans = min(temp, ans);
  }

  cout << ans << endl;
}


Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  int rousoku[n] = {};

  for (int i = 0; i < n; i++) {
    cin >> rousoku[i];
  }

  long long ans = 1e10;

  // 連続したろうそくを選択する方法がベスト
  for (int i = 0; i < n - k + 1; i++) {
    // 右から辿る方と左から辿る方
    long long l, r = 0;
    l = abs(rousoku[i]) + abs(rousoku[i] - rousoku[i + k - 1]);
    r = abs(rousoku[i + k - 1]) + abs(rousoku[i] - rousoku[i + k - 1]);
    long long temp = min(l, r);
    ans = min(temp, ans);
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stack>
#include <string.h>

using namespace std;

int N, K;
int x[20000];
int len[20000];

int main(int argc, char **argv) {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> x[i];
  int m = 1e9;
  for (int i = 0; i < N - K + 1; i++) {
    int len = x[i + K - 1] - x[i];
    int d = min(abs(x[i]), abs(x[i + K - 1]));
    int m2 = len + d;
    m = min(m, m2);
  }
  cout << m << endl;
  ;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stack>
#include <string.h>

using namespace std;

int N, K;
int x[200000];
int len[200000];

int main(int argc, char **argv) {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> x[i];
  int m = 1e9;
  for (int i = 0; i < N - K + 1; i++) {
    int len = x[i + K - 1] - x[i];
    int d = min(abs(x[i]), abs(x[i + K - 1]));
    int m2 = len + d;
    m = min(m, m2);
  }
  cout << m << endl;
  ;
}
Problem p03274 (C++)

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rREP(i, k, n) for (int i = (int)(n)-1; i >= k; i--)

#define fi first
#define se second

#define vi vector<int>
#define pb push_back
#define mp make_pair

#define pcnt __builtin_popcount

typedef long long ll;

const ll inf = 10010010010010010;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

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 main() {
  int n, k;
  cin >> n >> k;
  int x[10010];
  rep(i, n) cin >> x[i];
  ll mi = inf;
  rep(i, n - k + 1) {
    ll d = (ll)x[i + k - 1] - x[i];
    mi = min({llabs(x[i]) + d, llabs(x[i + k - 1]) + d, mi});
  }
  cout << mi << endl;
  return 0;
}

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rREP(i, k, n) for (int i = (int)(n)-1; i >= k; i--)

#define fi first
#define se second

#define vi vector<int>
#define pb push_back
#define mp make_pair

#define pcnt __builtin_popcount

typedef long long ll;

const ll inf = 10010010010010010;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

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 main() {
  int n, k;
  cin >> n >> k;
  int x[100100];
  rep(i, n) cin >> x[i];
  ll mi = inf;
  rep(i, n - k + 1) {
    ll d = (ll)x[i + k - 1] - x[i];
    mi = min({llabs(x[i]) + d, llabs(x[i + k - 1]) + d, mi});
  }
  cout << mi << endl;
  return 0;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 10010;
int n, k, a[N];

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int ans = 2e9 + 10;
  for (int i = 1; i + k - 1 <= n; i++) {
    if ((long long)a[i] * a[i + k - 1] < 0)
      ans = min(ans, (int)abs(a[i]) + (int)abs(a[i + k - 1]) +
                         min((int)abs(a[i]), (int)abs(a[i + k - 1])));
    else
      ans = min(ans, max((int)abs(a[i]), (int)abs(a[i + k - 1])));
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 100010;
int n, k, a[N];

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int ans = 2e9 + 10;
  for (int i = 1; i + k - 1 <= n; i++) {
    if ((long long)a[i] * a[i + k - 1] < 0)
      ans = min(ans, (int)abs(a[i]) + (int)abs(a[i + k - 1]) +
                         min((int)abs(a[i]), (int)abs(a[i + k - 1])));
    else
      ans = min(ans, max((int)abs(a[i]), (int)abs(a[i + k - 1])));
  }
  cout << ans;
  return 0;
}
Problem p03274 (C++)

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

using namespace std;

#define INF 1000000007
#define LINF (1LL << 60)
#define PI 3.14159265358979

typedef long long i64;
typedef pair<i64, i64> P;

inline i64 mod(i64 a, i64 m) { return (a % m + m) % m; }

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 n, k, a[101010];
void solve() {
  cin >> n >> k;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 0)
      k--;
    if (a[i] < 0)
      cnt++;
  }

  int ans = INF;
  if (cnt >= k)
    chmin(ans, abs(a[cnt - k]));
  if (n - cnt >= k)
    chmin(ans, a[cnt + k - 1]);
  for (int i = 1; i < k; i++) {
    if (cnt < k - i)
      continue;
    chmin(ans, max(a[cnt + i - 1], abs(a[cnt - k + i])) +
                   2 * min(a[cnt + i - 1], abs(a[cnt - k + i])));
    // cout << ans << " " << a[cnt+i-1] << " " << a[cnt-k+i] <<  endl;
    // cout << a[cnt+i-1]+abs(a[cnt-k+i])+2*min(a[cnt+i-1],abs(a[cnt-k+i])) <<
    // endl;
  }
  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

  return 0;
}


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

using namespace std;

#define INF 1000000007
#define LINF (1LL << 60)
#define PI 3.14159265358979

typedef long long i64;
typedef pair<i64, i64> P;

inline i64 mod(i64 a, i64 m) { return (a % m + m) % m; }

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 n, k, a[101010];
void solve() {
  cin >> n >> k;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 0)
      k--;
    if (a[i] < 0)
      cnt++;
  }

  int ans = INF;
  if (cnt >= k)
    chmin(ans, abs(a[cnt - k]));
  if (n - cnt >= k)
    chmin(ans, a[cnt + k - 1]);
  for (int i = 1; i < k; i++) {
    if (cnt < k - i)
      continue;
    if (cnt + i - 1 >= n)
      continue;
    chmin(ans, max(a[cnt + i - 1], abs(a[cnt - k + i])) +
                   2 * min(a[cnt + i - 1], abs(a[cnt - k + i])));
    // cout << ans << " " << a[cnt+i-1] << " " << a[cnt-k+i] <<  endl;
    // cout << a[cnt+i-1]+abs(a[cnt-k+i])+2*min(a[cnt+i-1],abs(a[cnt-k+i])) <<
    // endl;
  }
  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

  return 0;
}
Problem p03274 (C++)

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

int main() {
  int n, k;
  long long candles[100000];
  cin >> n >> k;
  int ydrs = -1;
  for (int i = 0; i < n; i++) {
    cin >> candles[i];
    if (candles != 0 && candles[i - 1] < 0 && candles[i] >= 0)
      ydrs = i;
  }
  if (ydrs == -1) {
    if (candles[n - 1] > 0)
      cout << candles[k - 1] << endl;
    else
      cout << -candles[n - k] << endl;
    return 0;
  }
  long long minn = 10E14;
  long long l = ydrs - k + 1, r = ydrs;
  for (; candles[l] <= 0; l++, r++) {
    minn = min(minn, 0 - 2 * candles[l] + candles[r]);
    minn = min(minn, 0 - candles[l] + 2 * candles[r]);
  }
  cout << minn << endl;
  return 0;
}

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

int main() {
  int n, k;
  long long candles[100000];
  cin >> n >> k;
  int ydrs = -1;
  for (int i = 0; i < n; i++) {
    cin >> candles[i];
    if (candles != 0 && candles[i - 1] < 0 && candles[i] >= 0)
      ydrs = i;
  }
  if (ydrs == -1) {
    if (candles[n - 1] > 0)
      cout << candles[k - 1] << endl;
    else
      cout << -candles[n - k] << endl;
    return 0;
  }
  long long minn = 10E14;
  long long l = ydrs - k + 1, r = ydrs;
  if (l < 0)
    l = 0, r = k - 1;
  for (; candles[l] <= 0 && r < n; l++, r++) {
    minn = min(minn, 0 - 2 * candles[l] + candles[r]);
    minn = min(minn, 0 - candles[l] + 2 * candles[r]);
  }
  cout << minn << endl;
  return 0;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPL(i, f, n) for (int i = f, i##_len = (n); i < i##_len; ++i)
typedef long long ll;

int func(int x) {
  if (x % 2 != 0)
    return 0;
  return 1 + func(x / 2);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, K, x[10010];
  int ans = 2 * 100000000;
  cin >> N >> K;
  REP(i, N) cin >> x[i];

  for (int i = 0; i <= N - K; i++) {
    int l = i;
    int r = i + K - 1;
    ans = min(ans, min(abs(x[l]), abs(x[r])) + abs(x[r] - x[l]));
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPL(i, f, n) for (int i = f, i##_len = (n); i < i##_len; ++i)
typedef long long ll;

int func(int x) {
  if (x % 2 != 0)
    return 0;
  return 1 + func(x / 2);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, K, x[100010];
  int ans = 3 * 100000000;
  cin >> N >> K;
  REP(i, N) cin >> x[i];

  for (int i = 0; i <= N - K; i++) {
    int l = i;
    int r = i + K - 1;
    ans = min(ans, min(abs(x[l]), abs(x[r])) + abs(x[r] - x[l]));
  }
  cout << ans << endl;
  return 0;
}
Problem p03274 (C++)

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> x(N);
  for (int i = 0; i < N; i++) {
    cin >> x.at(i);
  }
  int ans = 2000000000;
  if (x.at(0) >= 0) {

    ans = x.at(K - 1);

    cout << ans << endl;
  } else if (x.at(N - 1) <= 0) {

    ans = -x.at(N - K);

    cout << ans << endl;
  } else {
    int b = 0;
    for (int i = 0; i < N - K + 1; i++) {
      if (x.at(N - 1 + i) <= 0) {
        b = abs(x.at(0));
        ans = min(ans, b);
      } else if (x.at(i) >= 0) {
        b = x.at(K + i - 1);
        ans = min(ans, b);
      } else {
        b = min(2 * x.at(K + i - 1) - x.at(i), x.at(K + i - 1) - 2 * x.at(i));
        ans = min(ans, b);
      }
    }
    cout << ans << endl;
  }
}


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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> x(N);
  for (int i = 0; i < N; i++) {
    cin >> x.at(i);
  }
  int ans = 2000000000;
  if (x.at(0) >= 0) {

    ans = x.at(K - 1);

    cout << ans << endl;
  } else if (x.at(N - 1) <= 0) {

    ans = -x.at(N - K);

    cout << ans << endl;
  } else {
    int b = 0;
    for (int i = 0; i < N - K + 1; i++) {
      if (x.at(K - 1 + i) <= 0) {
        b = abs(x.at(0));
        ans = min(ans, b);
      } else if (x.at(i) >= 0) {
        b = x.at(K + i - 1);
        ans = min(ans, b);
      } else {
        b = min(2 * x.at(K + i - 1) - x.at(i), x.at(K + i - 1) - 2 * x.at(i));
        ans = min(ans, b);
      }
    }
    cout << ans << endl;
  }
}
Problem p03274 (C++)

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

int main() {

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

  /* 連続したK個を訪れるのが最短で、N個から連続したK個の選び方はN-K+1通り
     一気に+行って-に行くかその逆が最小値なのでN-K+1通りについて全探索*/

  int ans = 1e+9;

  // -に行ってから+に行く場合
  for (int i = 0; i < N - K + 1; i++) {
    ans = min(ans, abs(x[i]) + x[i + K - 1] - x[i]);
  }
  // +に行ってから-に行く場合
  for (int i = N - 1; i >= K - 1; i--) {
    ans = min(ans, abs(x[i]) + x[i] - x[i - K + 1]);
  }

  cout << ans << endl;

  return 0;
}

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

int main() {

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

  /* 連続したK個を訪れるのが最短で、N個から連続したK個の選び方はN-K+1通り
     一気に+行って-に行くかその逆が最小値なのでN-K+1通りについて全探索*/

  int ans = 1e+9;

  // -に行ってから+に行く場合
  for (int i = 0; i < N - K + 1; i++) {
    ans = min(ans, abs(x[i]) + x[i + K - 1] - x[i]);
  }
  // +に行ってから-に行く場合
  for (int i = N - 1; i >= K - 1; i--) {
    ans = min(ans, abs(x[i]) + x[i] - x[i - K + 1]);
  }

  cout << ans << endl;

  return 0;
}
Problem p03274 (C++)

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

long long int labs(long long int a, long long int b) {
  return ((a > b) ? a - b : b - a);
}

long long int lmin(long long int a, long long int b) {
  return ((a > b) ? b : a);
}

int main() {
  long long int n, k, x[10000];
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> x[i];
  }
  long long int ans = 1145141919;
  for (int i = k; i <= n; ++i) {
    ans = lmin(ans, labs(x[i - k] - x[i - 1]) +
                        lmin(labs(x[i - k]), labs(x[i - 1])));
  }
  cout << ans << endl;
  return 0;
}

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

long long int labs(long long int a, long long int b) {
  return ((a > b) ? a - b : b - a);
}

long long int lmin(long long int a, long long int b) {
  return ((a > b) ? b : a);
}

int main() {
  long long int n, k, x[100001];
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> x[i];
  }
  long long int ans = 1145141919;
  for (int i = k; i <= n; ++i) {
    ans = lmin(ans, labs(x[i - k] - x[i - 1]) +
                        lmin(labs(x[i - k]), labs(x[i - 1])));
  }
  cout << ans << endl;
  return 0;
}
Problem p03274 (C++)

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

int n, k;
vector<int> pos, neg;

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (x < 0)
      neg.push_back(-x);
    else
      pos.push_back(x);
  }
  sort(neg.begin(), neg.end());
  int ans = 1e9;
  if (pos.size() >= k)
    ans = min(ans, pos[k - 1]);
  if (neg.size() >= k)
    ans = min(ans, neg[k - 1]);
  for (int i = 1; i <= k; i++) {
    if (pos.size() < i)
      break;
    int tmp = k - i;
    if (neg.size() < tmp)
      continue;
    int co = pos[i - 1] + neg[tmp - 1] + min(pos[i - 1], neg[tmp - 1]);
    ans = min(ans, co);
  }
  printf("%d\n", ans);
  return 0;
}

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

int n, k;
vector<int> pos, neg;

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (x < 0)
      neg.push_back(-x);
    else
      pos.push_back(x);
  }
  sort(neg.begin(), neg.end());
  int ans = 1e9;
  if (pos.size() >= k)
    ans = min(ans, pos[k - 1]);
  if (neg.size() >= k)
    ans = min(ans, neg[k - 1]);
  for (int i = 1; i < k; i++) {
    if (pos.size() < i)
      break;
    int tmp = k - i;
    if (neg.size() < tmp)
      continue;
    int co = pos[i - 1] + neg[tmp - 1] + min(pos[i - 1], neg[tmp - 1]);
    ans = min(ans, co);
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03274 (C++)

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

int main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> sei;
  vector<long long> hu;
  long long sub;
  for (long long i = 0; i < n; i++) {
    cin >> sub;
    if (sub >= 0) {
      sei.push_back(sub);
    } else {
      hu.push_back(-1 * sub);
    }
  }
  if (hu.size() > 0) {
    reverse(hu.begin(), hu.end());
  }

  // まず...
  long long min = -1;
  if (m <= sei.size()) {
    min = sei[m - 1];
  }
  // 負の力を借りる
  for (long long i = 0; i <= (m - 1); i++) {

    // つまりm - i個負から借りる
    if ((m - i) <= hu.size()) {
      long long sus, sussus;
      long long minmin;
      sus = (i == 0) ? 0 : (sei[i - 1]);
      sussus = hu[m - i - 1];
      if (sus > sussus) {
        minmin = sus + sussus * 2;
      } else {
        minmin = sus * 2 + sussus;
      }
      if (minmin < min || min < 0) {
        min = minmin;
      }
    }
  }
  cout << min << endl;
}

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

int main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> sei;
  vector<long long> hu;
  long long sub;
  for (long long i = 0; i < n; i++) {
    cin >> sub;
    if (sub >= 0) {
      sei.push_back(sub);
    } else {
      hu.push_back(-1 * sub);
    }
  }
  if (hu.size() > 0) {
    reverse(hu.begin(), hu.end());
  }

  // まず...
  long long min = -1;
  if (m <= sei.size()) {
    min = sei[m - 1];
  }
  // 負の力を借りる
  for (long long i = 0; i <= (m - 1); i++) {

    // つまりm - i個負から借りる
    if ((m - i) <= hu.size() && i <= sei.size()) {
      long long sus, sussus;
      long long minmin;
      sus = (i == 0) ? 0 : (sei[i - 1]);
      sussus = hu[m - i - 1];
      if (sus > sussus) {
        minmin = sus + sussus * 2;
      } else {
        minmin = sus * 2 + sussus;
      }
      if (minmin < min || min < 0) {
        min = minmin;
      }
    }
  }
  cout << min << endl;
}
Problem p03274 (C++)

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> x(N), vec(K);
  for (int i = 0; i < N; i++) {
    cin >> x[i];
  }
  int ans = 1e9;
  for (int i = 0; i <= N - K; i++) {
    int min1, max_K, min_K;
    for (int j = 0; j < K; j++) {
      vec[j] = x[i + j];
    }
    max_K = *max_element(vec.begin(), vec.end());
    min_K = *min_element(vec.begin(), vec.end());
    if (min_K >= 0)
      min1 = max_K;
    else if (max_K <= 0)
      min1 = -min_K;
    else
      min1 = min(abs(min_K), max_K) * 2 + max(abs(min_K), max_K);
    ans = min(ans, min1);
  }
  cout << ans << endl;
}

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> x(N), vec(K);
  for (int i = 0; i < N; i++) {
    cin >> x[i];
  }
  int ans = 1e9;
  for (int i = 0; i <= N - K; i++) {
    int min1, max_K, min_K;
    max_K = x[i + K - 1];
    min_K = x[i];
    if (min_K >= 0)
      min1 = max_K;
    else if (max_K <= 0)
      min1 = -min_K;
    else
      min1 = min(abs(min_K), max_K) * 2 + max(abs(min_K), max_K);
    ans = min(ans, min1);
  }
  cout << ans << endl;
}
Problem p03274 (C++)

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

using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

const int Dist(const vector<int> &values, const int num) {
  if (values.empty() || num == 0) {
    return 0;
  } else {
    return values[num - 1];
  }
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> lefts, rights;
  for (int i = 0; i < N; ++i) {
    int candle;
    cin >> candle;
    if (candle == 0) {
      --K;
    } else if (candle < 0) {
      lefts.push_back(-candle);
    } else {
      rights.push_back(candle);
    }
  }
  sort(lefts.begin(), lefts.end());
  int minSum = MOD;
  for (int leftNum = max(0, K - static_cast<int>(rights.size())),
           n = lefts.size();
       leftNum <= n; ++leftNum) {
    int rightNum = K - leftNum;
    int sum = Dist(lefts, leftNum) + Dist(rights, rightNum);
    if (leftNum != 0 && rightNum != 0) {
      sum += min(lefts[leftNum - 1], rights[rightNum - 1]);
    }
    if (sum < minSum) {
      minSum = sum;
    }
  }
  cout << minSum << endl;
}


Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>

using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

const int Dist(const vector<int> &values, const int num) {
  if (values.empty() || num == 0) {
    return 0;
  } else {
    return values[num - 1];
  }
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> lefts, rights;
  for (int i = 0; i < N; ++i) {
    int candle;
    cin >> candle;
    if (candle == 0) {
      --K;
    } else if (candle < 0) {
      lefts.push_back(-candle);
    } else {
      rights.push_back(candle);
    }
  }
  sort(lefts.begin(), lefts.end());
  int minSum = MOD;
  for (int leftNum = max(0, K - static_cast<int>(rights.size())),
           n = lefts.size();
       leftNum <= n; ++leftNum) {
    int rightNum = K - leftNum;
    if (rightNum < 0) {
      break;
    }
    int sum = Dist(lefts, leftNum) + Dist(rights, rightNum);
    if (leftNum != 0 && rightNum != 0) {
      sum += min(lefts[leftNum - 1], rights[rightNum - 1]);
    }
    if (sum < minSum) {
      minSum = sum;
    }
  }
  cout << minSum << endl;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>

inline int readin() {
  int x = 0, c;
  bool f = 0;
  for (; (c = getchar()) < '0' || c > '9'; f |= c == '-')
    ;
  for (; c >= '0' && c <= '9'; c = getchar())
    x = (x << 1) + (x << 3) + c - 48;
  return f ? -x : x;
}

int a[200005];

int main() {
  int n = readin(), k = readin();
  int x = -233, ans = 2147483647;
  for (int i = 1; i <= n; i++) {
    a[i] = readin();
    if (!a[i])
      x = i;
    if (x == -233 && a[i] > 0) {
      x = i;
      a[i + 1] = a[i];
      a[i++] = 0;
      k++, n++;
    }
  }
  for (int i = x; i; i--)
    if (i + k - 1 <= n) {
      int tmp = a[i + k - 1] - a[i] * 2;
      if (tmp < ans)
        ans = tmp;
    }
  for (int i = x; i <= n; i++)
    if (i - k + 1 >= 1) {
      int tmp = a[i] * 2 - a[i - k + 1];
      if (tmp < ans)
        ans = tmp;
    }
  printf("%d", ans);
}


Fixed submission:
#include <cstdio>

inline int readin() {
  int x = 0, c;
  bool f = 0;
  for (; (c = getchar()) < '0' || c > '9'; f |= c == '-')
    ;
  for (; c >= '0' && c <= '9'; c = getchar())
    x = (x << 1) + (x << 3) + c - 48;
  return f ? -x : x;
}

int a[200005];

int main() {
  int n = readin(), k = readin();
  int x = -233, ans = 2147483647;
  for (int i = 1; i <= n; i++) {
    a[i] = readin();
    if (!a[i])
      x = i;
    if (x == -233 && a[i] > 0) {
      x = i;
      a[i + 1] = a[i];
      a[i++] = 0;
      k++, n++;
    }
  }
  if (x == -233)
    a[x = ++n] = 0, k++;
  for (int i = x; i; i--)
    if (i + k - 1 <= n) {
      int tmp = a[i + k - 1] - a[i] * 2;
      if (tmp < ans)
        ans = tmp;
    }
  for (int i = x; i <= n; i++)
    if (i - k + 1 >= 1) {
      int tmp = a[i] * 2 - a[i - k + 1];
      if (tmp < ans)
        ans = tmp;
    }
  printf("%d", ans);
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N, K, x[10001];
  cin >> N >> K;
  for (int i = 1; i <= N; ++i) {
    cin >> x[i];
  }
  long long res = 1 << 30;
  for (int l = 1; l <= N - K + 1; ++l) {
    int r = l + K - 1;
    if (abs(x[l]) > abs(x[r])) {
      res = min(res, (long long)(abs(x[r]) + x[r] - x[l]));
    } else {
      res = min(res, (long long)(abs(x[l]) + x[r] - x[l]));
    }
  }
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N, K, x[100001];
  cin >> N >> K;
  for (int i = 1; i <= N; ++i) {
    cin >> x[i];
  }
  long long res = 1 << 30;
  for (int l = 1; l <= N - K + 1; ++l) {
    int r = l + K - 1;
    if (abs(x[l]) > abs(x[r])) {
      res = min(res, (long long)(abs(x[r]) + x[r] - x[l]));
    } else {
      res = min(res, (long long)(abs(x[l]) + x[r] - x[l]));
    }
  }
  cout << res << endl;
  return 0;
}
Problem p03274 (C++)

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

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<int> px(1, 0), mx;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    if (x > 0)
      px.emplace_back(x);
    else
      mx.emplace_back(-x);
  }
  mx.emplace_back(0);
  reverse(mx.begin(), mx.end());
  int64_t ans = 1e18;
  for (int i = min(k, (int)px.size() - 1), j = k - i; j < mx.size(); --i, ++j) {
    ans = min(ans,
              min(1L * px.at(i) * 2 + mx.at(j), 1L * px.at(i) + mx.at(j) * 2));
  }
  cout << ans << "\n"s;
}


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

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<int> px(1, 0), mx;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    if (x > 0)
      px.emplace_back(x);
    else
      mx.emplace_back(-x);
  }
  mx.emplace_back(0);
  reverse(mx.begin(), mx.end());
  int64_t ans = 1e18;
  for (int i = min(k, max(0, (int)px.size() - 1)), j = k - i;
       i >= 0 && j < mx.size(); --i, ++j) {
    ans = min(ans,
              min(1L * px.at(i) * 2 + mx.at(j), 1L * px.at(i) + mx.at(j) * 2));
  }
  cout << ans << "\n"s;
}
Problem p03274 (C++)

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

int posv[100001];
int negv[100001];

int main() {
  int N, K;
  cin >> N >> K;
  int x[100001];
  int pos = 0;
  int neg = 0;
  for (int i = 1; i <= N; i++) {
    cin >> x[i];
    if (x[i] >= 0)
      pos++;
  }
  neg = N - pos;

  for (int i = 1; i <= pos; i++) {
    posv[i] = x[i + neg];
  }
  for (int i = 1; i <= neg; i++) {
    negv[i] = -1 * x[neg + 1 - i];
  }

  int MIN = 1e9;
  for (int i = 0; i <= pos; i++) {
    int num = 0;
    if (K - i <= neg) {
      num = ((posv[i] < negv[K - i]) + 1) * posv[i] +
            ((posv[i] >= negv[K - i]) + 1) * negv[K - i];
      MIN = min(MIN, num);
    }
  }
  cout << MIN << endl;

  return 0;
}

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

int posv[100001];
int negv[100001];

int main() {
  int N, K;
  cin >> N >> K;
  int x[100001];
  int pos = 0;
  int neg = 0;
  for (int i = 1; i <= N; i++) {
    cin >> x[i];
    if (x[i] >= 0)
      pos++;
  }
  neg = N - pos;

  for (int i = 1; i <= pos; i++) {
    posv[i] = x[i + neg];
  }
  for (int i = 1; i <= neg; i++) {
    negv[i] = -1 * x[neg + 1 - i];
  }

  int MIN = 1e9;
  int m = min(pos, K);
  for (int i = 0; i <= m; i++) {
    int num = 0;
    if (K - i <= neg) {
      num = ((posv[i] < negv[K - i]) + 1) * posv[i] +
            ((posv[i] >= negv[K - i]) + 1) * negv[K - i];
      MIN = min(MIN, num);
    }
  }
  cout << MIN << endl;

  return 0;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main(void) {

  int i, k, n, x_1 = 0, x_2 = 0, x_3 = 0, r_1 = 0, r_2 = 0, x[10000];
  long long int min = 1000000000;

  scanf("%d %d", &n, &k);

  for (i = 0; i < n; i++) {
    scanf("%d", &x[i]);
  }

  for (i = 0; i < n - k + 1; i++) {
    if (x[i] < 0) {
      x_1 = -x[i];
    } else {
      x_1 = x[i];
    }
    if (x[i + k - 1] < 0) {
      x_2 = -x[i + k - 1];
    } else {
      x_2 = x[i + k - 1];
    }
    if (x[i] - x[i + k - 1] < 0) {
      x_3 = -(x[i] - x[i + k - 1]);
    } else {
      x_3 = x[i] - x[i + k - 1];
    }
    r_1 = x_1 + x_3;
    if (r_1 < min) {
      min = r_1;
    }
    r_2 = x_2 + x_3;
    if (r_2 < min) {
      min = r_2;
    }
  }
  printf("%lld\n", min);

  return 0;
}

Fixed submission:
#include <stdio.h>
int main(void) {

  int i, k, n, x_1 = 0, x_2 = 0, x_3 = 0, r_1 = 0, r_2 = 0, x[100000];
  long long int min = 1000000000;

  scanf("%d %d", &n, &k);

  for (i = 0; i < n; i++) {
    scanf("%d", &x[i]);
  }

  for (i = 0; i < n - k + 1; i++) {
    if (x[i] < 0) {
      x_1 = -x[i];
    } else {
      x_1 = x[i];
    }
    if (x[i + k - 1] < 0) {
      x_2 = -x[i + k - 1];
    } else {
      x_2 = x[i + k - 1];
    }
    if (x[i] - x[i + k - 1] < 0) {
      x_3 = -(x[i] - x[i + k - 1]);
    } else {
      x_3 = x[i] - x[i + k - 1];
    }
    r_1 = x_1 + x_3;
    if (r_1 < min) {
      min = r_1;
    }
    r_2 = x_2 + x_3;
    if (r_2 < min) {
      min = r_2;
    }
  }
  printf("%lld\n", min);

  return 0;
}
Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

int main() {
  int n, k, x[1000], minus{};
  long long min = 100000000000000;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", x + i);
    if (x[i] < 0)
      minus = i;
  }

  for (int i = std::max(minus - k + 1, 0); i + k <= n; i++) {
    long long sum = std::abs(x[i] - x[i + k - 1]) +
                    std::min(std::abs(x[i]), std::abs(x[i + k - 1]));
    if (sum < min)
      min = sum;
  }
  printf("%lld\n", min);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

int main() {
  int n, k, x[100000], minus{};
  long long min = 100000000000000;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", x + i);
    if (x[i] < 0)
      minus = i;
  }

  for (int i = std::max(minus - k + 1, 0); i + k <= n; i++) {
    long long sum = std::abs(x[i] - x[i + k - 1]) +
                    std::min(std::abs(x[i]), std::abs(x[i + k - 1]));
    if (sum < min)
      min = sum;
  }
  printf("%lld\n", min);

  return 0;
}
Problem p03274 (C++)

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

#define REP(i, n, m) for (int i = n; i < m; i++)
#define REPR(i, n, m) for (int i = n; i >= m; i--)
#define LREP(i, n, m) for (ll i = n; i < m; i++)
#define LREPR(i, n, m) for (ll i = n; i >= m; i--)

#define INF 1e9 + 1;
#define MOD 1e9 + 7;

typedef long long ll;
// typedef pair<int, int > P;
// typedef pair<ll, ll > LP;

using namespace std;

int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  return b ? gcd(b, a % b) : a;
}

int N, K;

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

  cin >> N >> K;

  int x[N];
  int idx;

  REP(i, 0, N) cin >> x[i];

  REP(i, 0, N - 1) {
    if (x[i] <= 0 && x[i + 1] >= 0) {
      idx = i;
    }
  }

  if (x[0] >= 0) {
    cout << x[K - 1] << endl;
  } else if (x[N - 1] < 0) {
    cout << -x[N - K] << endl;
  } else {
    int start = max(0, idx - K + 1);
    int ans = INF;
    REP(i, start, start + K) {
      ans = min(ans, abs(x[i]) + 2 * abs(x[i + K - 1]));
      ans = min(ans, 2 * abs(x[i]) + abs(x[i + K - 1]));
    }
    cout << ans << endl;
  }
}


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

#define REP(i, n, m) for (int i = n; i < m; i++)
#define REPR(i, n, m) for (int i = n; i >= m; i--)
#define LREP(i, n, m) for (ll i = n; i < m; i++)
#define LREPR(i, n, m) for (ll i = n; i >= m; i--)

#define INF 1e9 + 1;
#define MOD 1e9 + 7;

typedef long long ll;
// typedef pair<int, int > P;
// typedef pair<ll, ll > LP;

using namespace std;

int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  return b ? gcd(b, a % b) : a;
}

int N, K;

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

  cin >> N >> K;

  int x[N];
  int idx;

  REP(i, 0, N) cin >> x[i];

  REP(i, 0, N - 1) {
    if (x[i] <= 0 && x[i + 1] >= 0) {
      idx = i;
    }
  }

  if (x[0] >= 0) {
    cout << x[K - 1] << endl;
  } else if (x[N - 1] < 0) {
    cout << -x[N - K] << endl;
  } else {
    int start = max(0, idx - K + 1);
    int ans = INF;
    REP(i, start, N - K + 1) {
      ans = min(ans, abs(x[i]) + 2 * abs(x[i + K - 1]));
      ans = min(ans, 2 * abs(x[i]) + abs(x[i + K - 1]));
    }
    cout << ans << endl;
  }
}
Problem p03274 (C++)

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

using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
#define ll long long
using lint = long long;
typedef pair<int, int> P;
const lint inf = 1000000007;
const int MOD = 1000000007;
signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> x(n);
  rep(i, n) { cin >> x[i]; }
  vector<int> pulse, minus;
  pulse.push_back(0);
  ++k;
  rep(i, n) {
    if (x[i] >= 0)
      pulse.push_back(x[i]);
    else
      minus.push_back(x[i]);
  }
  reverse(ALL(minus));
  vector<lint> msm(n, 0);
  vector<lint> psm(n, 0);
  rep(i, minus.size()) {
    msm[i + 1] = minus[i]; //+msm[i];
  }
  rep(i, pulse.size()) {
    psm[i + 1] = pulse[i]; //+psm[i];
  }
  lint res = inf * inf;
  rep1(i, min((int)pulse.size(), k)) {
    if (k - i > minus.size())
      continue;
    lint buf = 2 * psm[i] - msm[k - i];
    buf = min(buf, psm[i] - 2 * msm[k - i]);
    res = min(res, buf);
  }
  cout << res << "\n";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
#define ll long long
using lint = long long;
typedef pair<int, int> P;
const lint inf = 1000000007;
const int MOD = 1000000007;
signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> x(n);
  rep(i, n) { cin >> x[i]; }
  vector<int> pulse, minus;
  pulse.push_back(0);
  ++k;
  rep(i, n) {
    if (x[i] >= 0)
      pulse.push_back(x[i]);
    else
      minus.push_back(x[i]);
  }
  reverse(ALL(minus));
  vector<lint> msm(n + 5, 0);
  vector<lint> psm(n + 5, 0);
  rep(i, minus.size()) {
    msm[i + 1] = minus[i]; //+msm[i];
  }
  rep(i, pulse.size()) {
    psm[i + 1] = pulse[i]; //+psm[i];
  }
  lint res = inf * inf;
  rep1(i, min((int)pulse.size(), k)) {
    if (k - i > minus.size())
      continue;
    lint buf = 2 * psm[i] - msm[k - i];
    buf = min(buf, psm[i] - 2 * msm[k - i]);
    res = min(res, buf);
  }
  cout << res << "\n";
  return 0;
}