Problem p03276 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define F first
#define S second
#define vc vector
#define INF (0x3f3f3f3f)
#define LINF ((long long)(0x3f3f3f3f3f3f3f3f))
#define Pb push_back
#define Mp make_pair
#define ll long long
#define _tp template
#define _tyn typename
#define Pf push_front
#define ull unsigned ll
#define pii pair<int, int>
#define ld double
#define Sqr(x) ((x) * (x))
#define All(x) x.begin(), x.end()
#define B cout << "BreakPoint" << endl;
#define Fin(a) freopen(a, "r", stdin)
#define Fout(a) freopen(a, "w", stdout)
#define Ms(_data) memset(_data, 0, sizeof(_data))
#define Msn(_data, _num) memset(_data, _num, sizeof(_data))
#define O_(x) cout << #x << " " << x << "  ";
#define O(x) cout << #x << " " << x << endl;
using namespace std;
#define Mymax(a, b)                                                            \
  if (a < b)                                                                   \
    a = b;
#define Mymin(a, b)                                                            \
  if (a > b)                                                                   \
    a = b;
_tp<_tyn T> void Print(T _a[], int _s, int _t) {
  for (int i = _s; i <= _t; i++)
    cout << _a[i] << " ";
  cout << endl;
}
#define il inline
#define rg register
struct InputReader {
  static const int bs = 100000;
  char buf[bs];
  int p;
  il InputReader() : p(bs) {}
  il void Flush() {
    p = 0;
    fread(buf, 1, bs, stdin);
  }
  il char C() {
    if (p >= bs)
      Flush();
    return buf[p++];
  }
  int Getnum() {
    char ch = C();
    while (ch < '0' || ch > '9')
      ch = C();
    return (int)(ch - '0');
  }
  il int operator()() {
    int ans = 0;
    char ch = C();
    int fu = 1;
    while (ch < '0' || ch > '9') {
      if (ch == '-')
        fu = -1;
      ch = C();
    }
    while (ch >= '0' && ch <= '9') {
      ans *= 10;
      ans += ch - '0';
      ch = C();
    }
    return ans * fu;
  }
  ll Readll() {
    ll ans = 0LL;
    ll fu = 1;
    char ch = C();
    while (ch < '0' || ch > '9') {
      if (ch == '-')
        fu = -1;
      ch = C();
    }
    while (ch >= '0' && ch <= '9') {
      ans *= 10LL;
      ans += ch - '0';
      ch = C();
    }
    return ans * fu;
  }
  il void Readstring(string &x) {
    x.clear();
    char ch = C();
    while (!isdigit(ch) && !isalpha(ch))
      ch = C();
    while (isdigit(ch) || isalpha(ch)) {
      x += ch;
      ch = C();
    }
  }
  il void Readalpha(char &c) {
    c = C();
    while (!isalpha(c))
      c = C();
  }
  il void Specialread(char &c) {
    c = C();
    while (c != 'o' && c != 'x' && c != '?')
      c = C();
  }
} In;
il void Read(int &x) { x = In(); }
il void Read(int &x, int &y) {
  x = In();
  y = In();
}
il void Read(int &x1, int &x2, int &x3) {
  x1 = In();
  x2 = In();
  x3 = In();
}
il void Read(int &x1, int &x2, int &x3, int &x4) {
  x1 = In();
  x2 = In();
  x3 = In();
  x4 = In();
}
il void Read(ll &x) { x = In.Readll(); }
il void Read(ll &x, ll &y) {
  x = In.Readll();
  y = In.Readll();
}
il void Read(ll &x1, ll &x2, ll &x3) {
  x1 = In.Readll();
  x2 = In.Readll();
  x3 = In.Readll();
}
il void Read(ll &x1, ll &x2, ll &x3, ll &x4) {
  x1 = In.Readll();
  x2 = In.Readll();
  x3 = In.Readll();
  x4 = In.Readll();
}
clock_t _starttime, _endtime;
il void FILEIO() {
#ifdef InterestingLSY
  _starttime = clock();
  Fin("in.in");
#endif
}
il void END() {
#ifdef InterestingLSY
  _endtime = clock();
  double _timeuse = (_endtime - _starttime) * 1000.0 / CLOCKS_PER_SEC;
  printf("\n\nTime usage:\n%.0lf ms\n", _timeuse);
#endif
  exit(0);
}
#define For(i, j) for (rg int(i) = 1; (i) <= (j); (i)++)
#define For0(i, j) for (rg int(i) = 0; (i) < (j); (i)++)
#define Forx(i, j, k) for (rg int(i) = (j); (i) <= (k); (i)++)
#define Forstep(i, j, k, st) for (rg int(i) = (j); (i) <= (k); (i) += (st))
#define fOR(i, j) for (rg int(i) = (j); (i) >= 1; (i)--)
#define fOR0(i, j) for (rg int(i) = (j)-1; (i) >= 0; (i)--)
#define fORx(i, j, k) for (rg int(i) = (j); (i) >= (k); (i)--)
int dx[] = {0, -1, 0, 1};
int dy[] = {1, 0, -1, 0};
//			D L U  R
#define Mymod(a, b)                                                            \
  if (a >= b)                                                                  \
    a -= b;
#define Rmod(a, b) a = (a % b + b) % b
#define Dis2(x1, y1, x2, y2) (Sqr((x1) - (x2)) + Sqr((y1) - (y2)))
#define Dis(x1, y1, x2, y2) (sqrt(Dis2(x1, y1, x2, y2)))
#define ERR(x) cout << "ERR! #" << x << endl
#define Msz(x)                                                                 \
  cout << "Sizeof " << #x << " " << sizeof(x) / 1024 / 1024 << " MB" << endl;
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
#define MAXN 100010

int n, k;
int x[MAXN];

int main() {
  FILEIO();

  Read(n, k);
  For(i, n) Read(x[i]);
  For(i, n) if (x[i] == 0) {
    Forx(j, i, n) x[j] = x[j + 1];
    n--;
    k--;
    break;
  }
  if (!k) {
    printf("0\n");
    END();
  }

  if (x[1] > 0) {
    printf("%d\n", x[k]);
    END();
  }
  if (x[n] < 0) {
    printf("%d\n", -x[n - k + 1]);
    END();
  }

  int bef;
  For(i, n) if (x[i] < 0 && x[i + 1] > 0) {
    bef = i;
    break;
  }

  int ans = INF;
  if (bef - k + 1 >= 1)
    ans = -x[bef - k + 1];
  if (bef + 1 + k - 1 <= n)
    Mymin(ans, x[bef + k]);
  Forx(i, bef + 1, min(bef + k - 1, n)) {
    int ri = i, le = bef - (k - (i - bef)) + 1;
    int tans = min(x[ri], -x[le]) + x[ri] - x[le];
    // if( i == bef+3 ) O(tans);
    Mymin(ans, tans);
  }

  printf("%d\n", ans);

  END();
}


Fixed submission:
#include <bits/stdc++.h>
#define F first
#define S second
#define vc vector
#define INF (0x3f3f3f3f)
#define LINF ((long long)(0x3f3f3f3f3f3f3f3f))
#define Pb push_back
#define Mp make_pair
#define ll long long
#define _tp template
#define _tyn typename
#define Pf push_front
#define ull unsigned ll
#define pii pair<int, int>
#define ld double
#define Sqr(x) ((x) * (x))
#define All(x) x.begin(), x.end()
#define B cout << "BreakPoint" << endl;
#define Fin(a) freopen(a, "r", stdin)
#define Fout(a) freopen(a, "w", stdout)
#define Ms(_data) memset(_data, 0, sizeof(_data))
#define Msn(_data, _num) memset(_data, _num, sizeof(_data))
#define O_(x) cout << #x << " " << x << "  ";
#define O(x) cout << #x << " " << x << endl;
using namespace std;
#define Mymax(a, b)                                                            \
  if (a < b)                                                                   \
    a = b;
#define Mymin(a, b)                                                            \
  if (a > b)                                                                   \
    a = b;
_tp<_tyn T> void Print(T _a[], int _s, int _t) {
  for (int i = _s; i <= _t; i++)
    cout << _a[i] << " ";
  cout << endl;
}
#define il inline
#define rg register
struct InputReader {
  static const int bs = 100000;
  char buf[bs];
  int p;
  il InputReader() : p(bs) {}
  il void Flush() {
    p = 0;
    fread(buf, 1, bs, stdin);
  }
  il char C() {
    if (p >= bs)
      Flush();
    return buf[p++];
  }
  int Getnum() {
    char ch = C();
    while (ch < '0' || ch > '9')
      ch = C();
    return (int)(ch - '0');
  }
  il int operator()() {
    int ans = 0;
    char ch = C();
    int fu = 1;
    while (ch < '0' || ch > '9') {
      if (ch == '-')
        fu = -1;
      ch = C();
    }
    while (ch >= '0' && ch <= '9') {
      ans *= 10;
      ans += ch - '0';
      ch = C();
    }
    return ans * fu;
  }
  ll Readll() {
    ll ans = 0LL;
    ll fu = 1;
    char ch = C();
    while (ch < '0' || ch > '9') {
      if (ch == '-')
        fu = -1;
      ch = C();
    }
    while (ch >= '0' && ch <= '9') {
      ans *= 10LL;
      ans += ch - '0';
      ch = C();
    }
    return ans * fu;
  }
  il void Readstring(string &x) {
    x.clear();
    char ch = C();
    while (!isdigit(ch) && !isalpha(ch))
      ch = C();
    while (isdigit(ch) || isalpha(ch)) {
      x += ch;
      ch = C();
    }
  }
  il void Readalpha(char &c) {
    c = C();
    while (!isalpha(c))
      c = C();
  }
  il void Specialread(char &c) {
    c = C();
    while (c != 'o' && c != 'x' && c != '?')
      c = C();
  }
} In;
il void Read(int &x) { x = In(); }
il void Read(int &x, int &y) {
  x = In();
  y = In();
}
il void Read(int &x1, int &x2, int &x3) {
  x1 = In();
  x2 = In();
  x3 = In();
}
il void Read(int &x1, int &x2, int &x3, int &x4) {
  x1 = In();
  x2 = In();
  x3 = In();
  x4 = In();
}
il void Read(ll &x) { x = In.Readll(); }
il void Read(ll &x, ll &y) {
  x = In.Readll();
  y = In.Readll();
}
il void Read(ll &x1, ll &x2, ll &x3) {
  x1 = In.Readll();
  x2 = In.Readll();
  x3 = In.Readll();
}
il void Read(ll &x1, ll &x2, ll &x3, ll &x4) {
  x1 = In.Readll();
  x2 = In.Readll();
  x3 = In.Readll();
  x4 = In.Readll();
}
clock_t _starttime, _endtime;
il void FILEIO() {
#ifdef InterestingLSY
  _starttime = clock();
  Fin("in.in");
#endif
}
il void END() {
#ifdef InterestingLSY
  _endtime = clock();
  double _timeuse = (_endtime - _starttime) * 1000.0 / CLOCKS_PER_SEC;
  printf("\n\nTime usage:\n%.0lf ms\n", _timeuse);
#endif
  exit(0);
}
#define For(i, j) for (rg int(i) = 1; (i) <= (j); (i)++)
#define For0(i, j) for (rg int(i) = 0; (i) < (j); (i)++)
#define Forx(i, j, k) for (rg int(i) = (j); (i) <= (k); (i)++)
#define Forstep(i, j, k, st) for (rg int(i) = (j); (i) <= (k); (i) += (st))
#define fOR(i, j) for (rg int(i) = (j); (i) >= 1; (i)--)
#define fOR0(i, j) for (rg int(i) = (j)-1; (i) >= 0; (i)--)
#define fORx(i, j, k) for (rg int(i) = (j); (i) >= (k); (i)--)
int dx[] = {0, -1, 0, 1};
int dy[] = {1, 0, -1, 0};
//			D L U  R
#define Mymod(a, b)                                                            \
  if (a >= b)                                                                  \
    a -= b;
#define Rmod(a, b) a = (a % b + b) % b
#define Dis2(x1, y1, x2, y2) (Sqr((x1) - (x2)) + Sqr((y1) - (y2)))
#define Dis(x1, y1, x2, y2) (sqrt(Dis2(x1, y1, x2, y2)))
#define ERR(x) cout << "ERR! #" << x << endl
#define Msz(x)                                                                 \
  cout << "Sizeof " << #x << " " << sizeof(x) / 1024 / 1024 << " MB" << endl;
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
#define MAXN 100010

int n, k;
int x[MAXN];

int main() {
  FILEIO();

  Read(n, k);
  For(i, n) Read(x[i]);
  For(i, n) if (x[i] == 0) {
    Forx(j, i, n) x[j] = x[j + 1];
    n--;
    k--;
    break;
  }
  if (!k) {
    printf("0\n");
    END();
  }

  if (x[1] > 0) {
    printf("%d\n", x[k]);
    END();
  }
  if (x[n] < 0) {
    printf("%d\n", -x[n - k + 1]);
    END();
  }

  int bef;
  For(i, n) if (x[i] < 0 && x[i + 1] > 0) {
    bef = i;
    break;
  }

  int ans = INF;
  if (bef - k + 1 >= 1)
    ans = -x[bef - k + 1];
  if (bef + 1 + k - 1 <= n)
    Mymin(ans, x[bef + k]);
  Forx(i, bef + 1, min(bef + k - 1, n)) {
    int ri = i, le = bef - (k - (i - bef)) + 1;
    if (le < 1 || ri > n || le > n || ri < 1)
      continue;
    int tans = min(x[ri], -x[le]) + x[ri] - x[le];
    // if( i == bef+3 ) O(tans);
    Mymin(ans, tans);
  }

  printf("%d\n", ans);

  END();
}