#include <iostream>                                                                                                                                                                                                                                            
#include <vector>                                                                                                                                                                                                                                              
#include <cstdlib>                                                                                                                                                                                                                                             
#include <algorithm>                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                               
using namespace std;                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                               
int min_val(int xl, int xr) {                                                                                                                                                                                                                                  
  int min1 = abs(xl) + abs(xl - xr);                                                                                                                                                                                                                           
  int min2 = abs(xr) + abs(xr - xl);                                                                                                                                                                                                                           
  if (min1 < min2) {                                                                                                                                                                                                                                           
    return min1 ;                                                                                                                                                                                                                                              
  } else {                                                                                                                                                                                                                                                     
    return min2;                                                                                                                                                                                                                                               
  }                                                                                                                                                                                                                                                            
}                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                               
int main() {                                                                                                                                                                                                                                                   
  vector<int> keiro;                                                                                                                                                                                                                                           
  vector<int> X;                                                                                                                                                                                                                                               
  int N,K;                                                                                                                                                                                                                                                     
  cin >> N >> K;                                                                                                                                                                                                                                               
  int x;                                                                                                                                                                                                                                                       
  for (int i=0;i<N;i++) {                                                                                                                                                                                                                                      
    cin >> x;                                                                                                                                                                                                                                                  
    X.push_back(x);                                                                                                                                                                                                                                            
  }                                                                                                                                                                                                                                                            
  for (int i=0; i < N-K+1;i++) {                                                                                                                                                                                                                               
    keiro.push_back(min_val(X[i], X[i+K-1]));                                                                                                                                                                                                                  
  }                                                                                                                                                                                                                                                            
  cout << *std::min_element(keiro.begin(), keiro.end());                                                                                                                                                                                                       
}                                                             

Measured memory_bytes: 1144