Problem p03276 (C++)

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

#define ll long long
#define lf long double
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<bool> vb;
typedef vector<vector<ll>> vv;
typedef vector<vector<vector<ll>>> vvv;
typedef vector<string> vs;
typedef vector<lf> vf;
#define tr1(x) cerr << #x << ": " << x << endl
#define tr2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define tr3(x, y, z)                                                           \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << endl
#define tr4(x, y, z, w)                                                        \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << " | " << #w << ": " << w << endl
#define trv(a)                                                                 \
  cerr << #a << ": ";                                                          \
  for (ll zz = 0; zz < a.size(); zz++)                                         \
    cerr << a[zz] << " ";                                                      \
  cerr << endl
#define trset(s)                                                               \
  cerr << #s << ": ";                                                          \
  for (auto &i : s)                                                            \
    cerr << i << " ";                                                          \
  cerr << "\n"
#define mod 1000000007ll
#define inf 9999999999999999ll
#define nl endl

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, k;
  cin >> n >> k;
  vi a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll ans = inf;
  ll index = lower_bound(a.begin(), a.end(), 0) - a.begin();
  // index,index+1,.....n-1
  for (ll i = index; i <= n - 1; i++) {
    ll dist = a[i];
    ll num = i - index + 1;
    ll left = k - num;
    // index-1,index-2,...index-left;
    if (left > 0) {
      dist += a[i] - a[index - left];
      ans = min(ans, dist);
    } else
      ans = min(ans, dist);
  }
  for (ll i = 0; i < n; i++) {
    a[i] = -a[i];
  }
  reverse(a.begin(), a.end());
  index = lower_bound(a.begin(), a.end(), 0) - a.begin();
  for (ll i = index; i <= n - 1; i++) {
    ll dist = a[i];
    ll num = i - index + 1;
    ll left = k - num;
    // index-1,index-2,...index-left;
    if (left > 0) {
      dist += a[i] - a[index - left];
      ans = min(ans, dist);
    } else
      ans = min(ans, dist);
  }
  cout << ans << nl;
}


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

#define ll long long
#define lf long double
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<bool> vb;
typedef vector<vector<ll>> vv;
typedef vector<vector<vector<ll>>> vvv;
typedef vector<string> vs;
typedef vector<lf> vf;
#define tr1(x) cerr << #x << ": " << x << endl
#define tr2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define tr3(x, y, z)                                                           \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << endl
#define tr4(x, y, z, w)                                                        \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << " | " << #w << ": " << w << endl
#define trv(a)                                                                 \
  cerr << #a << ": ";                                                          \
  for (ll zz = 0; zz < a.size(); zz++)                                         \
    cerr << a[zz] << " ";                                                      \
  cerr << endl
#define trset(s)                                                               \
  cerr << #s << ": ";                                                          \
  for (auto &i : s)                                                            \
    cerr << i << " ";                                                          \
  cerr << "\n"
#define mod 1000000007ll
#define inf 9999999999999999ll
#define nl endl

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, k;
  cin >> n >> k;
  vi a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll ans = inf;
  ll i = 0;
  ll j = k - 1;
  while (j < n) {
    ll p1 = a[i];
    ll p2 = a[j];
    ll d1 = abs(0 - a[i]) + abs(a[i] - a[j]);
    ll d2 = abs(0 - a[j]) + abs(a[i] - a[j]);
    ans = min(ans, min(d1, d2));
    i++;
    j++;
  }
  cout << ans << nl;
}