using ll=long long;
#include<limits>
namespace pcl{constexpr int INF=std::numeric_limits<int>::max()/2;constexpr ll LNF=std::numeric_limits<ll>::max()/2;constexpr double DNF=std::numeric_limits<double>::max()/2;constexpr double EPS=1e-8;constexpr int DX[]={1,0,-1,0,1,1,-1,-1};constexpr int DY[]={0,1,0,-1,-1,1,1,-1};}
#include<algorithm>
#include<cmath>
namespace pcl{template<typename T>void updmax(T&a,T const&b){a=std::max(a,b);}template<typename T>void updmin(T&a,T const&b){a=std::min(a,b);}template<typename T,typename U,typename V>bool in_range(T const&begin,U const&mid,V const&end){return begin<=mid&&mid<end;}template<typename T,typename U,typename V>bool in_range(T const&begin,std::initializer_list<U>list,V const&end){auto p=std::minmax_element(list.begin(),list.end());return begin<=*p.first&&*p.second<end;}inline bool eqdbl(double a,double b){return std::abs(a-b)<EPS;}inline bool ledbl(double a,double b){return a<b||eqdbl(a,b);}inline bool gtdbl(double a,double b){return a>b||eqdbl(a,b);}}
#include<iostream>
#include<string>
namespace pcl{template<typename Printer>void _assert_failed(char const*expr,Printer showmsg,char const*file,unsigned line){std::cerr<<"!!!!!Assertion Failed!!!!!"<<std::endl;std::cerr<<"--->`"<<expr<<"` at "<<file<<",line "<<line<<std::endl;showmsg();abort();}
#define DEFINE_ASSERT_XX(xx,cond_op)\
template<typename T,typename U>\
void _assert_##xx(char const*expr1_str,char const*expr2_str,\
T const&expr1,U const&expr2,char const*msg,\
char const*file,unsigned line){\
if(!(expr1 cond_op expr2)){\
std::string es=\
std::string(expr1_str)+" " #cond_op " "+expr2_str;\
auto printer=[&](){\
if(msg[0]!=0)std::cerr<<"|"<<msg<<std::endl;\
std::cerr<<"|left:"<<expr1<<std::endl;\
std::cerr<<"|right:"<<expr2<<std::endl;\
};\
_assert_failed(es.c_str(),printer,file,line);\
}\
}
DEFINE_ASSERT_XX(eq,==)DEFINE_ASSERT_XX(ne,!=)DEFINE_ASSERT_XX(gt,>)DEFINE_ASSERT_XX(lt,<)DEFINE_ASSERT_XX(ge,>=)DEFINE_ASSERT_XX(le,<=)template<typename T,typename U,typename V>void _assert_range(T const&begin,char const*mid_str,U const&mid,V const&end,char const*msg,char const*file,unsigned line){if(!in_range(begin,mid,end)){std::string es="";es+=std::to_string(begin)+"<="+mid_str+"("+std::to_string(mid)+")<"+std::to_string(end);auto printer=[&](){std::cerr<<"|variable is not in range."<<std::endl;if(msg[0]!=0)std::cerr<<"|"<<msg<<std::endl;};_assert_failed(es.c_str(),printer,file,line);}}
#undef DEFINE_ASSERT_XX
}
#ifdef NOASSERT
#define ASSERT(...)
#define ASSERT_RANGE(...)
#define ASSERT_EQ(...)
#define ASSERT_NE(...)
#define ASSERT_GT(...)
#define ASSERT_LT(...)
#define ASSERT_GE(...)
#define ASSERT_LE(...)
#else
#define ASSERT(expr,msg)\
(void)(!(expr)&&(pcl::_assert_failed(#expr,\
[&](){\
if((msg)[0]!=0)\
std::cerr<<"|"<<msg \
<<std::endl;\
},\
__FILE__,__LINE__),\
0))
#define ASSERT_RANGE(begin,mid,end,msg)\
pcl::_assert_range(begin,#mid,mid,end,msg,__FILE__,__LINE__)
#define CALL_ASSERT_XX(xx,expr1,expr2,msg)\
pcl::_assert_##xx(#expr1,#expr2,expr1,expr2,msg,__FILE__,__LINE__)
#define ASSERT_EQ(expr1,expr2,msg)CALL_ASSERT_XX(eq,expr1,expr2,msg)
#define ASSERT_NE(expr1,expr2,msg)CALL_ASSERT_XX(ne,expr1,expr2,msg)
#define ASSERT_GT(expr1,expr2,msg)CALL_ASSERT_XX(gt,expr1,expr2,msg)
#define ASSERT_LT(expr1,expr2,msg)CALL_ASSERT_XX(lt,expr1,expr2,msg)
#define ASSERT_GE(expr1,expr2,msg)CALL_ASSERT_XX(ge,expr1,expr2,msg)
#define ASSERT_LE(expr1,expr2,msg)CALL_ASSERT_XX(le,expr1,expr2,msg)
#endif
#ifdef PA_DEBUG
#define PD(stmt)stmt
#else
#define PD(stmt)
#endif
#ifdef PA_DEBUG_LIB
#define PD_LIB(stmt)stmt
#else
#define PD_LIB(stmt)
#endif
#include<iostream>
#include<vector>
namespace pcl{template<typename T>std::ostream&operator<<(std::ostream&os,std::vector<T>const&v){os<<'[';for(size_t i=0;i<v.size();i++){if(i!=0)os<<",";os<<v[i];}os<<']';return os;}template<typename T>std::istream&operator>>(std::istream&is,std::vector<T>&v){for(size_t i=0;i<v.size();i++)is>>v[i];return is;}}
#include<bits/stdc++.h>
using namespace std;using namespace pcl;int calc(vector<int>const&p,vector<int>const&m,int K,int ret){if(ret>(int)p.size()-1)return INF;if(K-ret>(int)m.size()-1)return INF;int cost_p=0,cost_m=0;for(int i=1;i<=ret;i++){cost_p+=p[i]-p[i-1];}for(int i=1;i<=K-ret;i++){cost_m+=m[i]-m[i-1];}return min(cost_p*2+cost_m,cost_m*2+cost_p);}int main(){int N,K;cin>>N>>K;vector<int>v(N);cin>>v;vector<int>p(1),m(1);for(auto x:v){if(x>=0)p.push_back(x);else m.push_back(-x);}sort(p.begin(),p.end());sort(m.begin(),m.end());int cost=INF;for(int i=0;i<=K;i++){updmin(cost,calc(p,m,K,i));}cout<<cost<<endl;}

Measured memory_bytes: 1404