#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std; using namespace __gnu_pbds;
//#pragma GCC optimize("Ofast") //#pragma GCC optimize "unroll-loops" //#pragma GCC optimize "prefetch-loop-arrays" //#pragma GCC target "sse,sse2,sse3,sse4,abm,avx,aes,sse4a,sse4.1,sse4.2,mmx,popcnt,tune=native"
#define foru(i,a,b) for(int i=(a);i<(b);i++)
#define ford(i,a,b) for(int i=(a);i>=(b);i--)
#define fori(a,b) foru(i,a,b)
#define forj(a,b) foru(j,a,b)
#define fork(a,b) foru(k,a,b)
#define seto(x,i) memset(x,i,sizeof x)
#define pf first
#define ps second
#define pb push_back
#define eb emplace_back
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
#define clz __builtin_clz
#define clzll __builtin_clzll
#define ctz __builtin_ctz
#define ctzll __builtin_ctzll
#define P2(x) (1LL<<(x))
#define sz(x) (int)x.size()
#define all(x) begin(x),end(x)
#if __SIZEOF_INT128__
    typedef __int128_t i128; typedef __uint128_t ui128;
#else
    typedef long long i128; typedef unsigned long long ui128;
#endif
typedef int64_t ll;  typedef uint64_t ull; typedef int8_t byte; typedef long double lld;
typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef pair<lld,lld> pdd;
template<typename T1,typename T2> using ordered_map=tree<T1,T2,less<int>,rb_tree_tag,tree_order_statistics_node_update>;
template<typename T1> using ordered_set=ordered_map<T1,null_type>;
const int inf=0x3f3f3f3f,MOD=1e9+7; const ll INF=0x3f3f3f3f3f3f3f3f; const lld PI=acos((lld)-1);
ll gcd(ll a, ll b){return !b?a:gcd(b,a%b);}
ll fpow(ll a,ll b){if(!b) return 1; ll t=fpow(a,b/2); if(b&1) return t*t%MOD*a%MOD; return t*t%MOD;}
const ll SEED=chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count();
mt19937 randgen(SEED); int randint(int a, int b){return uniform_int_distribution<int>(a,b)(randgen);} ll randll(ll a, ll b){return uniform_int_distribution<ll>(a,b)(randgen);}
template<class T1,class T2>constexpr const auto _min(const T1&x,const T2&y){return x<y?x:y;} template<class T,class...Ts>constexpr auto _min(const T&x,const Ts&...xs){return _min(x,_min(xs...));}
template<class T1,class T2>constexpr const auto _max(const T1&x,const T2&y){return x>y?x:y;} template<class T,class...Ts>constexpr auto _max(const T&x,const Ts&...xs){return _max(x,_max(xs...));}
#define min(...) _min(__VA_ARGS__)
#define max(...) _max(__VA_ARGS__)
struct pair_hash{template<class T1,class T2> size_t operator()(const pair<T1,T2>&pair)const{return 31*hash<T1>{}(pair.first)+hash<T2>{}(pair.second);}};
struct chash{int operator()(ll x) const { x+=0x9e3779b97f4a7c15; x=(x^(x>>30))*0xbf58476d1ce4e5b9; x=(x^(x>>27))*0x94d049bb133111eb; return x^(x>>31)+SEED;}};
#define nl "\n"

const int N=1000010,M=MOD;
int n,K,L,op[N],s[N],ans,j,l,r,mid,dp[N],x;
char ch;

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    //freopen("", "r", stdin); //freopen("", "w", stdout);
    cin>>n>>K>>L; ans=inf;
    fori(1,n+1){
        cin>>ch; s[i]=(ch>='a');
    }
    fork(0,2){
        fori(0,n+1) s[i]^=1;
        l=0; r=N;
        while(l<r){
            mid=(l+r)/2;
            fori(1,n+1){
                dp[i]=dp[i-1]+s[i]; op[i]=op[i-1]; j=max(0,i-L);
                if(dp[i]>dp[j]+mid){
                    dp[i]=dp[j]+mid;
                    op[i]=op[j]+1;
                }
            }
            if(op[n]<=K){
                r=mid;
            }
            else l=mid+1;
        }
        ans=min(ans,round(dp[n]-r*(K-op[n])));
    }
    cout<<ans;
    return 0;
}
/**
15 2 3
XXXxxXXXxxXXXxxXXX

15 2 3
xxxXXxxxXXxxxXXxxx
*/


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

const int N = 1000005;

int n, k, l;
int a[N], dp[N], op[N];
int pref[N], pref_op[N];

int calc(int cost) {
	for (int i = 1; i <= n; i++) {
		dp[i] = dp[i - 1] + a[i];
		op[i] = op[i - 1];
		if (i < l && dp[i] < i - cost) {
			dp[i] = i - cost;
			op[i] = 1;
		}
		if (i >= l && dp[i] < dp[i - l] + l - cost) {
			dp[i] = dp[i - l] + l - cost;
			op[i] = op[i - l] + 1;
		}
	}
	return op[n];
}

int solve() {
	int le = 0, ri = l;

	int res = 0;
	while (ri >= le) {
		int mid = (le + ri) / 2;
		if (calc(mid) <= k) {
			res = mid;
			ri = mid - 1;
		} else le = mid + 1;
	}

	int foo = calc(res);

	return dp[n] + k * res;
}

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

	cin >> n >> k >> l;
	string s;
	cin >> s;

	for (int i = 1; i <= n; i++) {
		a[i] = (islower(s[i - 1]) > 0);
	}

	int ans = solve();

	for (int i = 1; i <= n; i++)
		a[i] = 1 - a[i];

	ans = max(ans, solve());

	cout << n - ans << endl;

	return 0;
}


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

const int N = 1000005;

int n, k, l;
int a[N], dp[N], op[N];
int pref[N], pref_op[N];

int calc(int cost) {
	for (int i = 1; i <= n; i++) {
		dp[i] = dp[i - 1] + a[i];
		op[i] = op[i - 1];
		if (i < l && dp[i] < i - cost) {
			dp[i] = i - cost;
			op[i] = 1;
		}
		if (i >= l && dp[i] < dp[i - l] + l - cost) {
			dp[i] = dp[i - l] + l - cost;
			op[i] = op[i - l] + 1;
		}
	}
	return op[n];
}

int solve() {
	int le = 0, ri = l + 1;

	int res = 0;
	while (ri >= le) {
		int mid = (le + ri) / 2;
		if (calc(mid) <= k) {
			res = mid;
			ri = mid - 1;
		} else le = mid + 1;
	}

	int foo = calc(res);

	return dp[n] + k * res;
}

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

	cin >> n >> k >> l;
	string s;
	cin >> s;

	for (int i = 1; i <= n; i++) {
		a[i] = (islower(s[i - 1]) > 0);
	}

	int ans = solve();

	for (int i = 1; i <= n; i++)
		a[i] = 1 - a[i];

	ans = max(ans, solve());

	cout << n - ans << endl;

	return 0;
}


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

const int N = 1000005;

int n, k, l;
int a[N], dp[N], op[N];
int pref[N], pref_op[N];

int calc(int cost) {
	for (int i = 1; i <= n; i++) {
		dp[i] = dp[i - 1] + a[i];
		op[i] = op[i - 1];
		if (i < l && dp[i] < i - cost) {
			dp[i] = i - cost;
			op[i] = 1;
		}
		if (i >= l && dp[i] < dp[i - l] + l - cost) {
			dp[i] = dp[i - l] + l - cost;
			op[i] = op[i - l] + 1;
		}
	}
	return op[n];
}

int solve() {
	int l = 0, r = l + 1;

	int res = 0;
	while (r >= l) {
		int mid = (l + r) / 2;
		if (calc(mid) <= k) {
			res = mid;
			r = mid - 1;
		} else l = mid + 1;
	}

	int foo = calc(res);

	return dp[n] + k * res;
}

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

	cin >> n >> k >> l;
	string s;
	cin >> s;

	for (int i = 1; i <= n; i++) {
		a[i] = (islower(s[i - 1]) > 0);
	}

	int ans = solve();

	for (int i = 1; i <= n; i++)
		a[i] = 1 - a[i];

	ans = max(ans, solve());

	cout << n - ans << endl;

	return 0;
}


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

const int N = 1000005;

int n, k, l;
int a[N], dp[N], op[N];
int pref[N], pref_op[N];

int calc(int cost) {
	for (int i = 1; i <= n; i++) {
		dp[i] = dp[i - 1] + a[i];
		op[i] = op[i - 1];
		if (i < l && dp[i] < i - cost) {
			dp[i] = i - cost;
			op[i] = 1;
		}
		if (i >= l && dp[i] < dp[i - l] + l - cost) {
			dp[i] = dp[i - l] + l - cost;
			op[i] = op[i - l] + 1;
		}
	}
	return op[n];
}

int solve() {
	int l = 0, r = 2 * n;

	int res = 0;
	while (r >= l) {
		int mid = (l + r) / 2;
		if (calc(mid) <= k) {
			res = mid;
			r = mid - 1;
		} else l = mid + 1;
	}

	int foo = calc(res);

	return dp[n] + k * res;
}

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

	cin >> n >> k >> l;
	string s;
	cin >> s;

	for (int i = 1; i <= n; i++) {
		a[i] = (islower(s[i - 1]) > 0);
	}

	int ans = solve();

	for (int i = 1; i <= n; i++)
		a[i] = 1 - a[i];

	ans = max(ans, solve());

	cout << n - ans << endl;

	return 0;
}


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

const int N = 1000005;

int n, k, l;
int a[N], dp[N], op[N];
int pref[N], pref_op[N];

int calc(int cost) {
	for (int i = 1; i <= n; i++) {
		dp[i] = dp[i - 1] + a[i];
		op[i] = op[i - 1];
		if (i < l && dp[i] < i - cost) {
			dp[i] = i - cost;
			op[i] = 1;
		}
		if (i >= l && dp[i] < dp[i - l] + l - cost) {
			dp[i] = dp[i - l] + l - cost;
			op[i] = op[i - l] + 1;
		}
	}
	return op[n];
}

int solve() {
	int le = 0, ri = l;

	int res = 0;
	while (ri >= le) {
		int mid = (le + ri) / 2;
		if (calc(mid) <= k) {
			res = mid;
			ri = mid - 1;
		} else le = mid + 1;
	}

	int foo = calc(res);

	return dp[n] + k * res;
}

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

	cin >> n >> k >> l;
	string s;
	cin >> s;

	for (int i = 1; i <= n; i++) {
		a[i] = (islower(s[i - 1]) > 0);
	}

	int ans = solve();

	for (int i = 1; i <= n; i++)
		a[i] = 1 - a[i];

	ans = max(ans, solve());

	cout << n - ans << endl;

	return 0;
}


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

const int N = 1000005;

int n, k, l;
int a[N], dp[N], op[N];

int calc(int cost) {
	for (int i = 1; i <= n; i++) {
		dp[i] = dp[i - 1] + a[i];
		op[i] = op[i - 1];
		if (i >= l && dp[i] < dp[i - l] + l - cost) {
			dp[i] = dp[i - l] + l - cost;
			op[i] = op[i - l] + 1;
		}
	}
	return op[n];
}

int solve() {
	int l = 0, r = 2 * n;

	int res = 0;
	while (r >= l) {
		int mid = (l + r) / 2;
		if (calc(mid) <= k) {
			res = mid;
			r = mid - 1;
		} else l = mid + 1;
	}

	int foo = calc(res);

	return dp[n] + k * res;
}

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

	cin >> n >> k >> l;
	string s;
	cin >> s;

	for (int i = 1; i <= n; i++) {
		a[i] = (islower(s[i - 1]) > 0);
	}

	int ans = solve();

	for (int i = 1; i <= n; i++)
		a[i] = 1 - a[i];

	ans = max(ans, solve());

	cout << n - ans << endl;

	return 0;
}


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

const int N = 1000005;

int n, k, l;
int a[N], dp[N], op[N];
int pref[N], pref_op[N];

int calc(int cost) {
	for (int i = 1; i <= n; i++) {
		dp[i] = dp[i - 1] + a[i];
		op[i] = op[i - 1];
		if (i < l && dp[i] < i) {
			dp[i] = i;
			op[i] = 1;
		}
		if (i >= l && dp[i] < dp[i - l] + l - cost) {
			dp[i] = dp[i - l] + l - cost;
			op[i] = op[i - l] + 1;
		}
	}
	return op[n];
}

int solve() {
	int l = 0, r = 2 * n;

	int res = 0;
	while (r >= l) {
		int mid = (l + r) / 2;
		if (calc(mid) <= k) {
			res = mid;
			r = mid - 1;
		} else l = mid + 1;
	}

	int foo = calc(res);

	return dp[n] + k * res;
}

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

	cin >> n >> k >> l;
	string s;
	cin >> s;

	for (int i = 1; i <= n; i++) {
		a[i] = (islower(s[i - 1]) > 0);
	}

	int ans = solve();

	for (int i = 1; i <= n; i++)
		a[i] = 1 - a[i];

	ans = max(ans, solve());

	cout << n - ans << endl;

	return 0;
}


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

const int N = 1000005;

int n, k, l;
int a[N], dp[N], op[N];
int pref[N], pref_op[N];

int calc(int cost) {
	for (int i = 1; i <= n; i++) {
		dp[i] = dp[i - 1] + a[i];
		op[i] = op[i - 1];
		if (i < l && dp[i] < i - cost) {
			dp[i] = i - cost;
			op[i] = 1;
		}
		if (i >= l && dp[i] < dp[i - l] + l - cost) {
			dp[i] = dp[i - l] + l - cost;
			op[i] = op[i - l] + 1;
		}
	}
	return op[n];
}

int solve() {
	int le = 0, ri = l - 1;

	int res = 0;
	while (ri >= le) {
		int mid = (le + ri) / 2;
		if (calc(mid) <= k) {
			res = mid;
			ri = mid - 1;
		} else le = mid + 1;
	}

	int foo = calc(res);

	return dp[n] + k * res;
}

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

	cin >> n >> k >> l;
	string s;
	cin >> s;

	for (int i = 1; i <= n; i++) {
		a[i] = (islower(s[i - 1]) > 0);
	}

	int ans = solve();

	for (int i = 1; i <= n; i++)
		a[i] = 1 - a[i];

	ans = max(ans, solve());

	cout << n - ans << endl;

	return 0;
}


#include<bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define ll long long
#define ld double
#define endl '\n'

using namespace std;

mt19937 gen(chrono::system_clock::now().time_since_epoch().count());

const int N = 2e6 + 7;
const int M = 1e9 + 7;

int n, k, l, x[N];
int dp[N];
int cnt[N];
int cnt2[N];
string s;

pair<int,int> solve(int delta){
    for (int i = 1; i <= n; i++){
        int p = max(0, i - l);
        if (dp[p] + delta <= dp[i - 1] + x[i]) cnt[i] = cnt[p] + 1, dp[i] = dp[p] + delta;
        else cnt[i] = cnt[i - 1], dp[i] = dp[i - 1] + x[i];
    }
    for (int i = 1; i <= n; i++){
        int p = max(0, i - l);
        if (dp[p] + delta < dp[i - 1] + x[i]) cnt2[i] = cnt2[p] + 1, dp[i] = dp[p] + delta;
        else cnt2[i] = cnt2[i - 1], dp[i] = dp[i - 1] + x[i];
    }
    return {dp[n] - min(cnt[n], k)*delta, cnt2[n]};
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // LOCAL
    cin >> n >> k >> l;
    cin >> s;
    for (int i = 0; i < n; i++) x[i + 1] = ('a' <= s[i] && s[i] <= 'z');
    int res = n;
    int l = 0, r = N;
    while (l <= r){
        int m = (l + r)>>1;
        pair<int,int> cur = solve(m);
        if (cur.S <= k) res = min(res, cur.F), r = m - 1;
        else l = m + 1;
    }
    for (int i = 0; i < n; i++) x[i + 1] ^= 1;
    l = 0; r = N;
    while (l <= r){
        int m = (l + r)>>1;
        pair<int,int> cur = solve(m);
        if (cur.S <= k) res = min(res, cur.F), r = m - 1;
        else l = m + 1;
    }
    cout << res;
    return 0;
}


#include<bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define ll long long
#define ld double
#define endl '\n'

using namespace std;

mt19937 gen(chrono::system_clock::now().time_since_epoch().count());

const int N = 1e6 + 7;
const int M = 1e9 + 7;

int n, k, l, x[N];
int dp[N];
int cnt[N];
string s;

pair<int,int> solve(int delta){
    for (int i = 1; i <= n; i++){
        int p = max(0, i - l);
        if (dp[p] + delta <= dp[i - 1] + x[i]) cnt[i] = cnt[p] + 1, dp[i] = dp[p] + delta;
        else cnt[i] = cnt[i - 1], dp[i] = dp[i - 1] + x[i];
    }
    return {dp[n] - cnt[n]*delta, cnt[n]};
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // LOCAL
    cin >> n >> k >> l;
    cin >> s;
    for (int i = 0; i < n; i++) x[i + 1] = ('a' <= s[i] && s[i] <= 'z');
    int res = n;
    int l = 0, r = N;
    int now = N;
    while (l <= r){
        int m = (l + r)>>1;
        pair<int,int> cur = solve(m);
        if (cur.S <= k) now = min(now, m), r = m - 1;
        else l = m + 1;
    }
    pair<int,int> z = solve(now);
    res = min(res, z.F - (k - z.S)*(now - 1));
    for (int i = 0; i < n; i++) x[i + 1] ^= 1;
    l = 0; r = N; now = N;
    while (l <= r){
        int m = (l + r)>>1;
        pair<int,int> cur = solve(m);
        if (cur.S <= k) now = min(now, m), r = m - 1;
        else l = m + 1;
    }
    z = solve(now);
    res = min(res, z.F - (k - z.S)*(now - 1));
    cout << res;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define ll long long
#define pb push_back
typedef pair<int,int> pii;
const int maxn=1e6+10;
pii dp[maxn],stek[maxn];
int n,l,k,pref[maxn],niz[maxn],lp,rp;
bool operator <(pii x,pii y){
    if(x.ss<y.ss)return 1;
    else if(x.ss==y.ss){
        if(x.ff>y.ff)return 1;
    }
    return 0;
}
void update(pii &x,pii y){
    if(x<y)x=y;
}
pii calc(int x){

    memset(dp,0,sizeof(dp));
    for(int i=1;i<=n;i++){
        update(dp[i],{dp[max(0,i-l)].ff+1,dp[max(0,i-l)].ss+pref[i]-pref[max(0,i-l)]-x});
        update(dp[i],dp[i-1]);
    }

return dp[n];
}
int go(){

    for(int i=1;i<=n;i++)pref[i]=pref[i-1]+niz[i];

    pii pom=calc(0);

    ///printf("%d %d AAA\n",pom.ff,pom.ss);
    if(pom.ff<=k)return pref[n]-pom.ss;

    pii ret;
    int retsr;
    int l=0;
    int r=2*n;
    int sr;
    while(l<=r){
        sr=(l+r)/2;
        pii pom=calc(sr);

        if(pom.ff>=k){
            ret=pom;
            retsr=sr;
            l=sr+1;
        }
        else r=sr-1;
    }

    ///printf("%d %d HEHE\n",ret.ff,ret.ss);

    ret.ss=ret.ss+ret.ff*retsr-(ret.ff-k)*(retsr+1);

    return pref[n]-ret.ss;
}
int main(){

    ///freopen("test.txt","r",stdin);

    scanf("%d %d %d",&n,&k,&l);
    string s;
    cin>>s;

    for(int i=1;i<=n;i++){
        int pom;
        if(s[i-1]>='a' && s[i-1]<='z')pom=0;
        else pom=1;

        niz[i]=pom;
    }

    int rez;
    rez=go();

    for(int i=1;i<=n;i++)niz[i]^=1;

    rez=min(rez,go());

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

return 0;
}


#include <iostream>
#include <string>

using namespace std;

int n, m, *trees, *peoples, *ostart, *ofinish, *lens;
int START = -2 * 1000000000;
int FINISH = 2 * 1000000000;

void qs2(int *s_arr, int first, int last)
{
	if (first < last)
	{
		int left = first, right = last, middle = s_arr[(left + right) / 2];
		do
		{
			while (s_arr[left] < middle) left++;
			while (s_arr[right] > middle) right--;
			if (left <= right)
			{
				int tmp = s_arr[left];
				s_arr[left] = s_arr[right];
				s_arr[right] = tmp;

				int tmp1 = ostart[left];
				ostart[left] = ostart[right];
				ostart[right] = tmp1;

				int tmp2 = ofinish[left];
				ofinish[left] = ofinish[right];
				ofinish[right] = tmp2;

				left++;
				right--;
			}
		} while (left <= right);
		qs2(s_arr, first, right);
		qs2(s_arr, left, last);
	}
}
void qs2(int *s_arr, int size)
{
	qs2(s_arr, 0, size - 1);
}

void qs(int *s_arr, int first, int last)
{
	if (first < last)
	{
		int left = first, right = last, middle = s_arr[(left + right) / 2];
		do
		{
			while (s_arr[left] < middle) left++;
			while (s_arr[right] > middle) right--;
			if (left <= right)
			{
				int tmp = s_arr[left];
				s_arr[left] = s_arr[right];
				s_arr[right] = tmp;
				left++;
				right--;
			}
		} while (left <= right);
		qs(s_arr, first, right);
		qs(s_arr, left, last);
	}
}

void qs(int *s_arr, int size)
{
	qs(s_arr, 0, size - 1);
}

int max(int a1, int a2) { if (a1 > a2) return a1; else return a2; }
int min(int a1, int a2) { if (a1 < a2) return a1; else return a2; }

long long decision()
{
	long long res = 0;
	int j = 1; int k = 0, i1 = 0, i2 = 0, j1, j2;

	ostart = new int[2 * n];
	ofinish = new int[2 * n];
	lens = new int[2 * n];

	qs(trees, n);

	for (int i = 0; i < n; i++)
	{
		ostart[2 * i] = trees[i] - 1;
		ostart[2 * i + 1] = trees[i] + 1;
		if (i == 0)
		{
			ofinish[2 * i] = START - 1;
			if (i == n - 1)
				ofinish[2 * i + 1] = FINISH + 1;
			else
				ofinish[2 * i + 1] = (trees[i] + trees[i + 1]) / 2 + 1;

		}
		else if (i == n - 1)
		{
			if (i == 0)
				ofinish[2 * i] = START - 1;
			else
				ofinish[2 * i] = (trees[i - 1] + trees[i]) / 2;
			ofinish[2 * i + 1] = FINISH + 1;
		}
		else
		{
			ofinish[2 * i] = (trees[i - 1] + trees[i]) / 2;
			ofinish[2 * i + 1] = (trees[i] + trees[i + 1]) / 2 + 1;
		}
		lens[2 * i] = ostart[2 * i] - ofinish[2 * i];
		lens[2 * i+1] = ofinish[2 * i + 1] - ostart[2 * i + 1];
	}

	qs2(lens, 2*n);

	long long tm = m, tindex = 0;
	long long lindex = 0;
	long long kCurrent = 0;

	while (tm > 0)
	{
		while (lens[lindex] - kCurrent <= 0) lindex++;
		int c = 2 * n - lindex;
		tm -= c * (lens[lindex] - kCurrent);

		int ki = 0;
		for (; tindex < m && ki < c * (lens[lindex] - kCurrent); tindex++)
		{
			int lind = lindex + ki % c;
			int sh = kCurrent + ki / c;
			if (ostart[lind] > ofinish[lind])
			{
				peoples[tindex] = ostart[lind] - sh;
				res += ostart[lind] + 1 - peoples[tindex];
			}
			else
			{
				peoples[tindex] = ostart[lind] + sh;
				res += peoples[tindex] - (ostart[lind] - 1);
			}
			ki++;
		}
		kCurrent = lens[lindex];
	}

	return res;
}

int main()
{
	cin >> n >> m;
	trees = new int[n + 1];
	peoples = new int[m + 1];

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

	cout << decision() << endl;

	for (int i = 0; i < m; i++)
		cout << peoples[i] << " ";

	//system("pause");

	return 0;
}


#include <iostream>
#include <string>

using namespace std;

long long n, m, *trees, *peoples, *ostart, *ofinish, *lens;
long long START = -2 * 1000000000;
long long FINISH = 2 * 1000000000;

void qs2(long long *s_arr, int first, int last)
{
	if (first < last)
	{
		int left = first, right = last, middle = s_arr[(left + right) / 2];
		do
		{
			while (s_arr[left] < middle) left++;
			while (s_arr[right] > middle) right--;
			if (left <= right)
			{
				long long tmp = s_arr[left];
				s_arr[left] = s_arr[right];
				s_arr[right] = tmp;

				long long tmp1 = ostart[left];
				ostart[left] = ostart[right];
				ostart[right] = tmp1;

				long long tmp2 = ofinish[left];
				ofinish[left] = ofinish[right];
				ofinish[right] = tmp2;

				left++;
				right--;
			}
		} while (left <= right);
		qs2(s_arr, first, right);
		qs2(s_arr, left, last);
	}
}
void qs2(long long *s_arr, int size)
{
	qs2(s_arr, 0, size - 1);
}

void qs(long long *s_arr, int first, int last)
{
	if (first < last)
	{
		int left = first, right = last, middle = s_arr[(left + right) / 2];
		do
		{
			while (s_arr[left] < middle) left++;
			while (s_arr[right] > middle) right--;
			if (left <= right)
			{
				long long tmp = s_arr[left];
				s_arr[left] = s_arr[right];
				s_arr[right] = tmp;
				left++;
				right--;
			}
		} while (left <= right);
		qs(s_arr, first, right);
		qs(s_arr, left, last);
	}
}

void qs(long long *s_arr, int size)
{
	qs(s_arr, 0, size - 1);
}

int max(int a1, int a2) { if (a1 > a2) return a1; else return a2; }
int min(int a1, int a2) { if (a1 < a2) return a1; else return a2; }

long long decision()
{
	long long res = 0;
	int j = 1; int k = 0, i1 = 0, i2 = 0, j1, j2;

	ostart = new long long[2 * n];
	ofinish = new long long[2 * n];
	lens = new long long[2 * n];

	qs(trees, n);

	for (int i = 0; i < n; i++)
	{
		ostart[2 * i] = trees[i] - 1;
		ostart[2 * i + 1] = trees[i] + 1;
		if (i == 0)
		{
			ofinish[2 * i] = START - 1;
			if (i == n - 1)
				ofinish[2 * i + 1] = FINISH + 1;
			else
				ofinish[2 * i + 1] = (trees[i] + trees[i + 1]) / 2 + 1;

		}
		else if (i == n - 1)
		{
			if (i == 0)
				ofinish[2 * i] = START - 1;
			else
				ofinish[2 * i] = (trees[i - 1] + trees[i]) / 2;
			ofinish[2 * i + 1] = FINISH + 1;
		}
		else
		{
			ofinish[2 * i] = (trees[i - 1] + trees[i]) / 2;
			ofinish[2 * i + 1] = (trees[i] + trees[i + 1]) / 2 + 1;
		}
		lens[2 * i] = ostart[2 * i] - ofinish[2 * i];
		lens[2 * i+1] = ofinish[2 * i + 1] - ostart[2 * i + 1];
	}

	qs2(lens, 2*n);

	long long tm = m, tindex = 0;
	long long lindex = 0;
	long long kCurrent = 0;

	while (tm > 0)
	{
		while (lens[lindex] - kCurrent <= 0) lindex++;
		int c = 2 * n - lindex;
		tm -= c * (lens[lindex] - kCurrent);

		int ki = 0;
		for (; tindex < m && ki < c * (lens[lindex] - kCurrent); tindex++)
		{
			int lind = lindex + ki % c;
			int sh = kCurrent + ki / c;
			if (ostart[lind] > ofinish[lind])
			{
				peoples[tindex] = ostart[lind] - sh;
				res += ostart[lind] + 1 - peoples[tindex];
			}
			else
			{
				peoples[tindex] = ostart[lind] + sh;
				res += peoples[tindex] - (ostart[lind] - 1);
			}
			ki++;
		}
		kCurrent = lens[lindex];
	}

	return res;
}

int main()
{
	cin >> n >> m;
	trees = new long long[n + 1];
	peoples = new long long[m + 1];

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

	cout << decision() << endl;

	for (int i = 0; i < m; i++)
		cout << peoples[i] << " ";

	//system("pause");

	return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
int a[200010],c[200010];
struct m {
	int left;
	int len;
	int right;
}b[200010];
bool cmp(m a,m b)
{
	return a.len > b.len;
}
int main()
{
	int n, m;
	int i,ans=0;
	scanf("%d%d", &n,&m);
	for (i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
	}
	sort(a, a + n);
	for (i = 0; i+1 < n; i++)
	{
		b[i].left = a[i];
		b[i].len = a[i + 1] - a[i];
		b[i].right = a[i + 1];
	}
	sort(b, b + n - 1,cmp);
	int o=0,p=0;

	while (m > 0)
	{
		o = o + 1;
		if (m > 0)
		{
			m = m - 1;
			ans = ans + o;
			c[p++] = a[0] - o;
		}
		if (m > 0)
		{
			m = m - 1;
			ans = ans + o;
			c[p++] = a[n - 1] + o;
		}
		for (i = 0; b[i].len>=2&&i + 1 < n && m > 0; i++)
		{
			if (b[i].len > 2)
			{
				b[i].len = b[i].len - 2;
				m = m - 1;
				c[p++] = b[i].left + o;
				ans = ans + o;
				if (m <= 0)
					break;
				m = m - 1;
				ans = ans + o;
				c[p++] = b[i].right - o;
			}
			else if (b[i].len == 2)
			{
				m = m - 1;
				b[i].len = b[i].len - 1;
				ans = ans + o;
				c[p++] = b[i].left + o;
			}
		}

	}
	printf("%d\n", ans);
	for (i = 0; i<p; i++)
	{
		if (i == 0)
			printf("%d", c[i]);
		else
			printf(" %d", c[i]);
	}
	printf("\n");
	return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
int a[200010], c[200010];
struct m {
	int left;
	int len;
	int right;
}b[200010];
bool cmp(m a, m b)
{
	return a.len > b.len;
}
int main()
{
	int n, m;
	int i;
	long long int ans = 0;

	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
	}
	sort(a, a + n);
	for (i = 0; i + 1 < n; i++)
	{
		b[i].left = a[i];
		b[i].len = a[i + 1] - a[i];
		b[i].right = a[i + 1];
	}
	sort(b, b + n , cmp);
	int o = 0, p = 0;
	while (m > 0)
	{
		o = o + 1;
		for (i = 0; b[i].len>=2&&i + 1 < n && m > 0; i++)
		{
			if (b[i].len > 2)
			{
				b[i].len = b[i].len - 1;
				m = m - 1;
				c[p++] = b[i].left + o;
				ans = ans + o;
				if (m <= 0)
					break;
				b[i].len = b[i].len - 1;
				m = m - 1;
				ans = ans + o;
				c[p++] = b[i].right - o;
			}
			else if (b[i].len == 2)
			{
				m = m - 1;
				b[i].len = b[i].len - 1;
				ans = ans + o;
				c[p++] = b[i].left + o;
			}
		}
		if (m > 0)
		{
			m = m - 1;
			ans = ans + o;
			c[p++] = a[0] - o;
		}
		if (m > 0)
		{
			m = m - 1;
			ans = ans + o;
			c[p++] = a[n - 1] + o;
		}
	}
	printf("%lld\n", ans);
	for (i = 0; i < p; i++)
	{
		if (i == 0)
			printf("%d", c[i]);
		else
			printf(" %d", c[i]);
	}
	printf("\n");
	return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
int a[200010], c[200010];
struct m {
	int left;
	int len;
	int right;
}b[200010];
bool cmp(m a, m b)
{
	return a.len > b.len;
}
int main()
{
	int n, m;
	int i;
	long long int ans = 0;

	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
	}
	sort(a, a + n);
	for (i = 0; i + 1 < n; i++)
	{
		b[i].left = a[i];
		b[i].len = a[i + 1] - a[i];
		b[i].right = a[i + 1];
	}
	sort(b, b + n , cmp);
	int o = 0, p = 0;
	while (m > 0)
	{
		o = o + 1;
		for (i = 0; b[i].len>=2&&i + 1 < n && m > 0; i++)
		{
			if (b[i].len > 2)
			{
				b[i].len = b[i].len - 1;
				m = m - 1;
				c[p++] = b[i].left + o;
				ans = ans + o;
				if (m <= 0)
					break;
				b[i].len = b[i].len - 1;
				m = m - 1;
				ans = ans + o;
				c[p++] = b[i].right - o;
			}
			else if (b[i].len == 2)
			{
				m = m - 1;
				b[i].len = b[i].len - 1;
				ans = ans + o;
				c[p++] = b[i].left + o;
			}
		}
		if (m > 0)
		{
			m = m - 1;
			ans = ans + o;
			c[p++] = a[0] - o;
		}
		if (m > 0)
		{
			m = m - 1;
			ans = ans + o;
			c[p++] = a[n - 1] + o;
		}
	}
	printf("%d\n", ans);
	for (i = 0; i < p; i++)
	{
		if (i == 0)
			printf("%d", c[i]);
		else
			printf(" %d", c[i]);
	}
	printf("\n");
	return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
int a[200010], c[200010];
struct m {
	int left;
	int len;
	int right;
}b[200010];
bool cmp(m a, m b)
{
	return a.len > b.len;
}
int main()
{
	int n, m;
	int i, ans = 0;
	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
	}
	sort(a, a + n);
	for (i = 0; i + 1 < n; i++)
	{
		b[i].left = a[i];
		b[i].len = a[i + 1] - a[i];
		b[i].right = a[i + 1];
	}
	sort(b, b + n , cmp);
	int o = 0, p = 0;
	while (m > 0)
	{
		o = o + 1;
		for (i = 0; b[i].len>=2&&i + 1 < n && m > 0; i++)
		{
			if (b[i].len > 2)
			{
				b[i].len = b[i].len - 1;
				m = m - 1;
				c[p++] = b[i].left + o;
				ans = ans + o;
				if (m <= 0)
					break;
				b[i].len = b[i].len - 1;
				m = m - 1;
				ans = ans + o;
				c[p++] = b[i].right - o;
			}
			else if (b[i].len == 2)
			{
				m = m - 1;
				b[i].len = b[i].len - 1;
				ans = ans + o;
				c[p++] = b[i].left + o;
			}
		}
		if (m > 0)
		{
			m = m - 1;
			ans = ans + o;
			c[p++] = a[0] - o;
		}
		if (m > 0)
		{
			m = m - 1;
			ans = ans + o;
			c[p++] = a[n - 1] + o;
		}
	}
	printf("%d\n", ans);
	for (i = 0; i < p; i++)
	{
		if (i == 0)
			printf("%d", c[i]);
		else
			printf(" %d", c[i]);
	}
	printf("\n");
	return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
int a[200010],c[200010];
struct m {
	int left;
	int len;
	int right;
}b[200010];
bool cmp(m a,m b)
{
	return a.len > b.len;
}
int main()
{
	int n, m;
	int i,ans=0;
	scanf("%d%d", &n,&m);
	for (i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
	}
	sort(a, a + n);
	for (i = 0; i+1 < n; i++)
	{
		b[i].left = a[i];
		b[i].len = a[i + 1] - a[i];
		b[i].right = a[i + 1];
	}
	sort(b, b + n - 1,cmp);
	int o=0,p=0;
	while (m > 0)
	{
		o = o + 1;
		for (i = 0; i + 1 < n && m > 0; i++)
		{
			if (b[i].len > 2)
			{
				b[i].len = b[i].len - 2;
				m = m - 1;
				c[p++] = b[i].left + o;
				ans = ans + o;
				if (m <= 0)
					break;
				m = m - 1;
				ans = ans + o;
				c[p++] = b[i].right - o;
			}
			else if (b[i].len == 2)
			{
				m = m - 1;
				b[i].len = b[i].len - 1;
				ans = ans + o;
				c[p++] = b[i].left + o;
			}
		}
		if (m > 0)
		{
			m = m - 1;
			ans = ans + o;
			c[p++] = a[0] - o;
		}
		if (m > 0)
		{
			m = m - 1;
			ans = ans + o;
			c[p++] = a[n - 1] + o;
		}
	}
	printf("%d\n", ans);
	for (i = 0; i<p; i++)
	{
		if (i == 0)
			printf("%d", c[i]);
		else
			printf(" %d", c[i]);
	}
	printf("\n");
	return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
int a[200010], c[200010];
struct m {
	int left;
	int len;
	int right;
}b[200010];
bool cmp(m a, m b)
{
	return a.len > b.len;
}
int main()
{
	int n, m;
	int i, ans = 0;
	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
	}
	sort(a, a + n);
	for (i = 0; i + 1 < n; i++)
	{
		b[i].left = a[i];
		b[i].len = a[i + 1] - a[i];
		b[i].right = a[i + 1];
	}
	sort(b, b + n - 1, cmp);
	int o = 0, p = 0;
	while (m > 0)
	{
		o = o + 1;
		for (i = 0; b[i].len>=2&&i + 1 < n && m > 0; i++)
		{
			if (b[i].len > 2)
			{
				b[i].len = b[i].len - 2;
				m = m - 1;
				c[p++] = b[i].left + o;
				ans = ans + o;
				if (m <= 0)
					break;
				m = m - 1;
				ans = ans + o;
				c[p++] = b[i].right - o;
			}
			else if (b[i].len == 2)
			{
				m = m - 1;
				b[i].len = b[i].len - 1;
				ans = ans + o;
				c[p++] = b[i].left + o;
			}
		}
		if (m > 0)
		{
			m = m - 1;
			ans = ans + o;
			c[p++] = a[0] - o;
		}
		if (m > 0)
		{
			m = m - 1;
			ans = ans + o;
			c[p++] = a[n - 1] + o;
		}
	}
	printf("%d\n", ans);
	for (i = 0; i < p; i++)
	{
		if (i == 0)
			printf("%d", c[i]);
		else
			printf(" %d", c[i]);
	}
	printf("\n");
	return 0;
}


#include <iostream>
#include <fstream>
#include <map>
#include <unordered_map>
#include <set>
#include <string>
#include <algorithm>
#include <sstream>
#include <assert.h>
#include <bitset>
#include <iomanip>
#include <vector>
#include <queue>

#define en "\n"
#define for0(i, n) for(int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#pragma warning(disable : 4996)
typedef long long ll;
typedef long double ld;

using namespace std;

const int NMAX = 1e5 + 17;

ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}

vector <int> Del(int n) {
	vector<int> ans;
	int i = 1;
	for (i = 1; i * i < n; i++) {
		if (n % i == 0) {
			ans.pb(i);
			ans.pb(n / i);
		}
	}
	if (n % i == 0)
		ans.pb(i);
	sort(all(ans));
	return ans;
}

set<int> used;

void solve() {
	int n, m;
	cin >> n >> m;
	queue <pair<int, int>> q;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		q.push({ x + 1, 1 });
		q.push({ x - 1, 1 });
		used.insert(x);
	}
	ll ans = 0;
	vector<int> answer;
	int kol = 0;
	while (kol != m) {
		int to = q.front().first;
		int d = q.front().second;
		q.pop();
		if (used.count(to) == 0) {
			ans += d;
			answer.pb(to);
			kol++;
			q.push({ to + 1, d + 1 });
			q.push({ to - 1, d + 1 });
			used.insert(to);
		}
	}
	cout << ans << en;
	for (auto to : answer) {
		cout << to << " ";
	}

}

void main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	#ifdef _DEBUG
		freopen("in.txt", "r", stdin);
		freopen("out.txt", "w", stdout);
	#endif
		solve();
}


#include <iostream>
#include <vector>
#include <iomanip>
#include <cstring>
#include <string.h>
#include <iterator>
#include <math.h>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <tuple>
#include <limits.h>
#include <string>
#include <bitset>
#include <map>
#include <numeric>
#include<unordered_map>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define endl cout<<"\n"
#define lli long long int
#define ld long double
#define M_PI (3.14159265358979323846264338327950288)
#define MOD (1000000007)
#define pii pair<lli,lli>
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

using namespace std;
//using namespace __gnu_pbds;

//fast-exponentiation-lli
unsigned lli expo_fast(lli a, lli b) {
	a = a;
	lli result = 1;
	while (b) {
		//multiplyint(tos(result), a);
		if (b & 1)result = (result * a);
		b >>= 1;
		a = (a*a);
	}
	return (result);
}

void take_in(vector<lli> *arr) {
	for (int i = 0; i < arr->size(); i++)cin >> (*(arr))[i];
}

lli gcd(lli a, lli b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b);

}

/* Iterative Function to calculate (x^y)%p in O(log y) */
unsigned lli power(lli x, unsigned lli y, lli p)
{
	lli res = 1;      // Initialize result
	x = x % p;  // Update x if it is more than or equal to p
	while (y > 0) {
		// If y is odd, multiply x with result
		if (y & 1)res = (res*x) % p;
		// y must be even now
		y = y >> 1;
		x = (x*x) % p;
	}
	return res;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, m; cin >> n >> m;
	vector<lli>arr(n); take_in(&arr);
	map<lli, lli>visit;
	queue<lli>q;
	set<lli>tree;
	for (int i = 0; i < n; i++) {
		visit[arr[i]] = 1; tree.insert(arr[i]);
	}
	for (int i = 0; i < n; i++) {
		if (!visit[arr[i] + 1]) {
			q.push(arr[i] + 1);
			visit[arr[i] + 1] = 1;
		}
		if (!visit[arr[i] - 1]) {
			q.push(arr[i] - 1);
			visit[arr[i] - 1] = 1;
		}
	}
	lli cnt = 0;
	vector<lli>ans;
	while (cnt < m) {
		lli val = q.front();
		q.pop();
		visit[val] = 1;
		ans.push_back(val);
		cnt++;
		if (!visit[val + 1]) {
			q.push(val + 1); visit[val + 1] = 1;
		}
		if (!visit[val - 1]) {
			q.push(val - 1); visit[val - 1] = 1;
		}
	}
	lli a = 0;
	for (int i = 0; i < m; i++) {
		auto it = tree.upper_bound(ans[i]);
		if (it == tree.end()) {
			it--; a += (ans[i] - *it); continue;
		}
		lli tmp = abs(ans[i] - *it); it--;
		if (it != tree.end()) {
			tmp = min(tmp, abs(*it - ans[i]));
		}
		a += tmp;
	}
	cout << a << "\n";
	for (auto x : ans)cout << x << " ";
	//system("PAUSE");
}


#include <iostream>
#include<queue>
#include<map>
using namespace std;
long long t,n,a,b,k,s=0,sum=0;
queue<long long>q;
long long y[200005];
map<long long ,long long>m,l;
int main()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
    	cin>>a;
    	m[a]=1;
    	l[a]=1;
    	q.push(a);
    }
    while(sum<k)
    {
    	a=q.front();
    	q.pop();
    	if(m[a+1]==0)
    	{
    		m[a+1]=1;
    		sum++;
    		y[sum]=a+1;
    		s+=l[a];
    		q.push(a+1);
    		l[a+1]=l[a]+1;
    	}
    	if(sum==k)
    	break;
    	if(m[a-1]==0)
    	{
    		m[a-1]=1;
    		sum++;
    		y[sum]=a-1;
    		s+=l[a];
    		q.push(a-1);
    		l[a-1]=l[a]+1;
    	}
    }
    cout<<s<<endl;
    for(int i=1;i<=k;i++){
    cout<<y[i];
    if(i!=k)
        cout<<" ";}
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<unordered_map>
using namespace std;
#define M 1000000007
using pll = pair<long long int, long long int>;
using pii = pair<int, int>;
using pil = pair<int, long long int>;
using pli = pair<long long int, int>;
using piii = pair<pii, int>;
int n, m;
int a[200009];
unordered_map<int, int> b;
int queue[4000009];
int q, r;
int z[200009];
int rl;
int main() {
	int i, j, k;
	int t = 1, tv = 0;
	while (t--) {
		scanf("%d %d", &n, &m);
		for (i = 0; i < n; i++) {
			scanf("%d", &a[i]);
			b[a[i]] = 0;
		}
		sort(a, a + n);
		for (i = 0; i < n; i++) {
			queue[i] = a[i];
		}
		q = 0; r = n;
		long long int res = 0;
		while (q<r && rl<m) {
			int qi = queue[q++];
			//add near qi
			if (b.find(qi - 1) == b.end()) {
				b[qi - 1] = b[qi] + 1;
				res += b[qi - 1];
				z[rl++] = qi - 1;
				queue[r++] = qi - 1;
			}
			if (rl == m)break;
			if (b.find(qi + 1) == b.end()) {
				b[qi + 1] = b[qi] + 1;
				res += b[qi + 1];
				z[rl++] = qi + 1;
				queue[r++] = qi + 1;
			}
			if (rl == m)break;
		}
		printf("%lld\n", res);
		for (i = 0; i < rl; i++) {
			printf("%d ", z[i]);
		}
		printf("\n");
	}
}


#define _USE_MATH_DEFINES
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<utility>
#include<algorithm>
#include<climits>
#include<set>
#include<map>
#include<cmath>
#include<iomanip>
#include<iterator>
#include<queue>
#include<stack>
#include<cctype>
#include<deque>
#include<time.h>
#include<bitset>
#include<random>
#include<unordered_set>
#include<unordered_map>
#include<sstream>

//by Skeef79

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:16777216")
#define pb push_back
#define en '\n'
#define for0(i,n) for(int i = 0;i<n;i++)
#define for1(i,n) for(int i = 1;i<n;i++)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define vec vector
#define pii pair<int,int>
#define pll pair<ll,ll>
#define REP(a,i,b) for (int i = a;i<b;i++);

using namespace std;

const int INF = 2000000000;
const ll LINF = 2000000000000000000;

template<typename T> void print(vector<T>& a)
{
	for (int i = 0; i < a.size(); i++)
		cout << a[i] << ' ';
	cout << en;
}

template<typename T> void print(deque<T>& a)
{
	for (int i = 0; i < a.size(); i++)
		cout << a[i] << ' ';
	cout << en;
}

template<typename T> void print(vector<vector<T>>& a)
{
	for (int i = 0; i < a.size(); i++)
	{
		for (int j = 0; j < a[i].size(); j++)
			cout << a[i][j] << ' ';
		cout << en;
	}
}

template <typename T> void input(vector<T>& a)
{
	for (int i = 0; i < a.size(); i++)
		cin >> a[i];
}

template<typename T> void input(deque<T>& a)
{
	for (int i = 0; i < a.size(); i++)
		cin >> a[i];
}

template<typename T> void input(vector<vector<T>>& a)
{
	for (int i = 0; i < a.size(); i++)
		for (int j = 0; j < a[i].size(); j++)
			cin >> a[i][j];
}

struct tree
{
	int x, l, r;
};

bool operator<(tree a, tree b)
{
	return a.x < b.x;
}

void solve()
{
	ll n, m;
	cin >> n >> m;
	vector<ll>  a(n);
	input(a);
	set<ll> used;
	for (auto to : a)
		used.insert(to);

	vector<ll> ans;
	set<pair<ll, tree>> st;

	for (auto to : a)
	{
		pair<ll, tree> temp;
		temp.second.x = to;
		if (used.count(to + 1))
			temp.second.r = LINF;
		else
			temp.second.r = to + 1;
		if (used.count(to - 1))
			temp.second.l = LINF;
		else
			temp.second.l = to - 1;
		temp.first = min(abs(to - temp.second.r), abs(to - temp.second.l));

		st.insert(temp);
	}

	ll res = 0;

	while (m--)
	{
		auto temp = st.begin();
		pair<ll, tree> lol;
		lol.first = temp->first;
		lol.second.l = temp->second.l;
		lol.second.r = temp->second.r;
		lol.second.x = temp->second.x;

		bool rebuild = false;

		if (used.count(temp->second.l))
		{
			rebuild = true;
			lol.second.l--;
			if (used.count(lol.second.l))
				lol.second.l = LINF;
		}
		if (used.count(temp->second.r))
		{
			rebuild = true;
			lol.second.r++;
			if (used.count(lol.second.r))
				lol.second.r = LINF;
		}
		if (rebuild)
		{
			st.erase(temp);
			st.insert(lol);
			m++;
			continue;
		}
		int dist1 = abs(temp->second.x - temp->second.l);
		int dist2 = abs(temp->second.x - temp->second.r);

		if (dist1 < dist2)
		{
			ans.pb(temp->second.l);
			res += dist1;
			pair<ll, tree> kek;
			used.insert(temp->second.l);
			if (used.count(temp->second.l - 1))
				kek.second.l = LINF;
			else
				kek.second.l = temp->second.l - 1;

			kek.second.x = temp->second.x;
			kek.second.r = temp->second.r;
			kek.first = min(abs(kek.second.x - kek.second.r), abs(kek.second.x - kek.second.l));

			st.erase(temp);
			st.insert(kek);
		}
		else
		{
			ans.pb(temp->second.r);
			res += dist2;
			used.insert(temp->second.r);
			pair<ll, tree>kek;
			if (used.count(temp->second.r + 1))
				kek.second.r = LINF;
			else
				kek.second.r = temp->second.r + 1;

			kek.second.x = temp->second.x;
			kek.second.l = temp->second.l;
			kek.first = min(abs(kek.second.x - kek.second.r), abs(kek.second.x - kek.second.l));

			st.erase(temp);
			st.insert(kek);
		}
	}

	cout << res << en;
	print(ans);

}

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

#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
#endif

	//freopen("journey.in", "r", stdin);
	//freopen("journey.out", "w", stdout);

	int tst = 1;
	//cin >> tst;
	while (tst--)
		solve();

}


#define _USE_MATH_DEFINES
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<utility>
#include<algorithm>
#include<climits>
#include<set>
#include<map>
#include<cmath>
#include<iomanip>
#include<iterator>
#include<queue>
#include<stack>
#include<cctype>
#include<deque>
#include<time.h>
#include<bitset>
#include<random>
#include<unordered_set>
#include<unordered_map>
#include<sstream>

//by Skeef79

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:16777216")
#define pb push_back
#define en '\n'
#define for0(i,n) for(int i = 0;i<n;i++)
#define for1(i,n) for(int i = 1;i<n;i++)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define vec vector
#define pii pair<int,int>
#define pll pair<ll,ll>
#define REP(a,i,b) for (int i = a;i<b;i++);

using namespace std;

const int INF = 2000000000;
const ll LINF = 2000000000000000000;

template<typename T> void print(vector<T>& a)
{
	for (int i = 0; i < a.size(); i++)
		cout << a[i] << ' ';
	cout << en;
}

template<typename T> void print(deque<T>& a)
{
	for (int i = 0; i < a.size(); i++)
		cout << a[i] << ' ';
	cout << en;
}

template<typename T> void print(vector<vector<T>>& a)
{
	for (int i = 0; i < a.size(); i++)
	{
		for (int j = 0; j < a[i].size(); j++)
			cout << a[i][j] << ' ';
		cout << en;
	}
}

template <typename T> void input(vector<T>& a)
{
	for (int i = 0; i < a.size(); i++)
		cin >> a[i];
}

template<typename T> void input(deque<T>& a)
{
	for (int i = 0; i < a.size(); i++)
		cin >> a[i];
}

template<typename T> void input(vector<vector<T>>& a)
{
	for (int i = 0; i < a.size(); i++)
		for (int j = 0; j < a[i].size(); j++)
			cin >> a[i][j];
}

struct tree
{
	ll x, l, r;
};

bool operator<(tree a, tree b)
{
	return a.x < b.x;
}

void solve()
{
	ll n, m;
	cin >> n >> m;
	vector<ll>  a(n);
	input(a);
	set<ll> used;
	for (auto to : a)
		used.insert(to);

	vector<ll> ans;
	set<pair<ll, tree>> st;

	for (auto to : a)
	{
		pair<ll, tree> temp;
		temp.second.x = to;
		if (used.count(to + 1))
			temp.second.r = LINF;
		else
			temp.second.r = to + 1;
		if (used.count(to - 1))
			temp.second.l = LINF;
		else
			temp.second.l = to - 1;
		temp.first = min(abs(to - temp.second.r), abs(to - temp.second.l));

		st.insert(temp);
	}

	ll res = 0;

	while (m--)
	{
		auto temp = st.begin();
		pair<ll, tree> lol;
		lol.first = temp->first;
		lol.second.l = temp->second.l;
		lol.second.r = temp->second.r;
		lol.second.x = temp->second.x;

		bool rebuild = false;

		if (used.count(temp->second.l))
		{
			rebuild = true;
			lol.second.l--;
			if (used.count(lol.second.l))
				lol.second.l = LINF;
		}
		if (used.count(temp->second.r))
		{
			rebuild = true;
			lol.second.r++;
			if (used.count(lol.second.r))
				lol.second.r = LINF;
		}
		if (rebuild)
		{
			st.erase(temp);
			st.insert(lol);
			m++;
			continue;
		}
		ll dist1 = abs(temp->second.x - temp->second.l);
		ll dist2 = abs(temp->second.x - temp->second.r);

		if (dist1 < dist2)
		{
			ans.pb(temp->second.l);
			res += dist1;
			pair<ll, tree> kek;
			used.insert(temp->second.l);
			if (used.count(temp->second.l - 1))
				kek.second.l = LINF;
			else
				kek.second.l = temp->second.l - 1;

			kek.second.x = temp->second.x;
			kek.second.r = temp->second.r;
			kek.first = min(abs(kek.second.x - kek.second.r), abs(kek.second.x - kek.second.l));

			st.erase(temp);
			st.insert(kek);
		}
		else
		{
			ans.pb(temp->second.r);
			res += dist2;
			used.insert(temp->second.r);
			pair<ll, tree>kek;
			if (used.count(temp->second.r + 1))
				kek.second.r = LINF;
			else
				kek.second.r = temp->second.r + 1;

			kek.second.x = temp->second.x;
			kek.second.l = temp->second.l;
			kek.first = min(abs(kek.second.x - kek.second.r), abs(kek.second.x - kek.second.l));

			st.erase(temp);
			st.insert(kek);
		}
	}

	cout << res << en;
	print(ans);

}

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

#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
#endif

	//freopen("journey.in", "r", stdin);
	//freopen("journey.out", "w", stdout);

	int tst = 1;
	//cin >> tst;
	while (tst--)
		solve();

}


#define _USE_MATH_DEFINES
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<utility>
#include<algorithm>
#include<climits>
#include<set>
#include<map>
#include<cmath>
#include<iomanip>
#include<iterator>
#include<queue>
#include<stack>
#include<cctype>
#include<deque>
#include<time.h>
#include<bitset>
#include<random>
#include<unordered_set>
#include<unordered_map>
#include<sstream>

//by Skeef79

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:16777216")
#define pb push_back
#define en '\n'
#define for0(i,n) for(int i = 0;i<n;i++)
#define for1(i,n) for(int i = 1;i<n;i++)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define vec vector
#define pii pair<int,int>
#define pll pair<ll,ll>
#define REP(a,i,b) for (int i = a;i<b;i++);

using namespace std;

const int INF = 2000000000;
const ll LINF = 2000000000000000000;

template<typename T> void print(vector<T>& a)
{
	for (int i = 0; i < a.size(); i++)
		cout << a[i] << ' ';
	cout << en;
}

template<typename T> void print(deque<T>& a)
{
	for (int i = 0; i < a.size(); i++)
		cout << a[i] << ' ';
	cout << en;
}

template<typename T> void print(vector<vector<T>>& a)
{
	for (int i = 0; i < a.size(); i++)
	{
		for (int j = 0; j < a[i].size(); j++)
			cout << a[i][j] << ' ';
		cout << en;
	}
}

template <typename T> void input(vector<T>& a)
{
	for (int i = 0; i < a.size(); i++)
		cin >> a[i];
}

template<typename T> void input(deque<T>& a)
{
	for (int i = 0; i < a.size(); i++)
		cin >> a[i];
}

template<typename T> void input(vector<vector<T>>& a)
{
	for (int i = 0; i < a.size(); i++)
		for (int j = 0; j < a[i].size(); j++)
			cin >> a[i][j];
}

vector<ll> a;
ll n, m;

void m_bfs()
{
	set<ll> used;
	queue<int> q;
	map<ll, ll> d;

	for (auto to : a)
	{
		q.push(to);
		used.insert(to);
		d[to] = 0;
	}

	vector<ll> ans;
	ll res = 0;
	m += n;

	while (m--)
	{
		int v = q.front();
		q.pop();

		if (d[v] != 0)
		{
			ans.pb(v);
			res += d[v];
		}

		if (!used.count(v + 1))
		{
			q.push(v + 1);
			used.insert(v + 1);
			d[v + 1] = d[v] + 1;
		}
		if (!used.count(v - 1))
		{
			q.push(v - 1);
			used.insert(v - 1);
			d[v - 1] = d[v] + 1;
		}

	}
	cout << res << en;
	print(ans);

}

void solve()
{
	cin >> n >> m;
	a = vector<ll>(n);
	input(a);

	m_bfs();

}

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

#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
#endif

	//freopen("journey.in", "r", stdin);
	//freopen("journey.out", "w", stdout);

	int tst = 1;
	//cin >> tst;
	while (tst--)
		solve();

}


#include <algorithm>
#include <functional>
#include <cmath>
#include <limits>
#include <cfloat>
#include <cstdio>
#include <iomanip>
#include <ios>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <array>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

typedef int64_t I64;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef pair<int, int> pi;

#define fr(i,a,b) for (int i = a; i <= b; ++i)
#define fr2(i,a,b) for (int i = a; i < b; ++i)

int main() {
	ios_base::sync_with_stdio(false);

	int n, m;
	cin >> n >> m;

	vi x(n);
	fr2(i, 0, n) cin >> x[i];

	map<int, int> d;
	queue<int> q;
	vi ans;

	for (int xi : x) {
		q.push(xi);
		d[xi] = 0;
	}

	I64 sum = 0;
	while (!q.empty()) {
		if (ans.size() >= m) break;
		int v = q.front();
		q.pop();
		if (d[v] != 0) {
			ans.push_back(v);
			sum += d[v];
		}
		if (!d.count(v - 1)) {
			d[v - 1] = d[v] + 1;
			q.push(v - 1);
		}
		if (!d.count(v + 1)) {
			d[v + 1] = d[v] + 1;
			q.push(v + 1);
		}
	}

	cout << sum << "\n";
	for (int ai : ans) cout << ai << " ";

	return 0;
}


#include <algorithm>
#include <functional>
#include <cmath>
#include <limits>
#include <cfloat>
#include <cstdio>
#include <iomanip>
#include <ios>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <array>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

typedef int64_t I64;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef pair<int, int> pi;

#define fr(i,a,b) for (int i = a; i <= b; ++i)
#define fr2(i,a,b) for (int i = a; i < b; ++i)

int main() {
	ios_base::sync_with_stdio(false);

	int n, m;
	cin >> n >> m;

	vi x(n);
	fr2(i, 0, n) cin >> x[i];

	sort(x.begin(), x.end());

	vi y(m);
	int h = m / n, size = 0, ll = 0, lr = 0;
	fr2(i, 0, n) {
		if (size >= m) {
			break;
		}

		int hl = h / 2;
		int d1 = (i > 0)
			? min(h / 2, max((x[i] - x[i - 1] - 1) / 2, 0))
			: h / 2;
		fr(dx, 1, d1) {
			y[size++] = x[i] - dx;
			if (i == 0) ll++;
		}

		if (size >= m) {
			break;
		}

		int hr = h - hl;
		int d2 = (i < n - 1)
			? min(hr, max((int)ceil((x[i + 1] - x[i] - 1) / 2.0), 0))
			: hr;
		fr(dx, 1, d2) {
			y[size++] = x[i] + dx;
			if (i == n - 1) lr++;
		}
	}

	int left = m - size;
	int x1 = x[0], xn = x[n - 1];
	fr(dx, 1, left) {
		if (size < m) {
			y[size++] = x1 - ll - dx;
		}
		if (size < m) {
			y[size++] = xn + lr + dx;
		}
	}

	sort(y.begin(), y.end());

	I64 sum = 0;
	int j = 0;
	fr2(i, 0, m) {
		int d1 = abs(x[j] - y[i]);
		if (j < n - 1) {
			int d2 = abs(x[j + 1] - y[i]);
			if (d2 < d1) {
				++j;
			}
		}
		sum += abs(x[j] - y[i]);
	}

	cout << sum << '\n';
	fr2(i, 0, m) cout << y[i] << " ";

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <bitset>
#include <unordered_set>
#include <cstdlib>
#include <ctime>
using namespace std;

long long n, m, x[200100];

bool check_ans( long long r ) {
	long long ans = 0;
	for (long long i = 2; i <= n; i++) {
		long long mn = min(x[i] - x[i - 1] - 1, 2 * r);
		ans += max( mn, 0ll);
	}
	if (ans + 2 * r >= m) return true;
	return false;
}

long long bp() {
	long long r = 2e9 + 3;
	long long l = 0;
	while (r - l > 1) {
		long long m = (r + l) / 2;
		if (check_ans(m)) {
			r = m;
		}
		else {
			l = m;
		}
	}
	return r;
}

int  main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	cin >> n >> m;
	x[0] = -2e9;
	x[n + 1] = 2e9;
	for (long long i = 1; i <= n; i++) {
		cin >> x[i];
	}
	sort(x + 1, x + n + 1);
	long long rs = bp();
	long long q = 0;
	vector < pair < int, int > > p;
	for (int i = 2; i <= n; i++) {
		if (x[i] - x[i - 1] - 1 >= 2 * rs) {
			for (int j = 1; j <= rs; j++) {
				p.push_back(make_pair(j, x[i] - j));
				p.push_back(make_pair(j, x[i - 1] + j));
			}
		}
		else {
			for (int j = x[i - 1] + 1; j < x[i]; j++) {
				p.push_back(make_pair(min(abs(x[i - 1] - j), abs(x[i] - j)), j));
			}
		}
	}
	for (int i = 1; i <= rs; i++) {
		p.push_back(make_pair(i, x[1] - i));
		p.push_back(make_pair(i, x[n] + i));
	}
	sort(p.begin(), p.end());
	for (int i = 0; i < m; i++) {
		q += p[i].first;
	}
	cout << q << endl;
	for (int i = 0; i < m; i++) {
		cout << p[i].second << ' ';
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <bitset>
#include <unordered_set>
#include <cstdlib>
#include <ctime>
using namespace std;

long long n, m, x[200100];

bool check_ans( long long r ) {
	long long ans = 0;
	for (long long i = 2; i <= n; i++) {
		long long mn = min(x[i] - x[i - 1] - 1, 2 * r);
		ans += max( mn, 0ll);
	}
	if (ans + 2 * r >= m) return true;
	return false;
}

long long bp() {
	long long r = 2e9 + 3;
	long long l = 0;
	while (r - l > 1) {
		long long m = (r + l) / 2;
		if (check_ans(m)) {
			r = m;
		}
		else {
			l = m;
		}
	}
	return r;
}

int  main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	cin >> n >> m;
	x[0] = -2e9;
	x[n + 1] = 2e9;
	for (long long i = 1; i <= n; i++) {
		cin >> x[i];
	}
	sort(x + 1, x + n + 1);
	long long rs = bp();
	long long q = 0;
	vector < int > p;
	for (int i = 2; i <= n; i++) {
		if (x[i] - x[i - 1] - 1 >= 2 * rs) {
			for (int j = 1; j <= rs; j++) {
				q += 2 * j;
				p.push_back(x[i] - j);
				p.push_back(x[i - 1] + j);
			}
		}
		else {
			for (int j = x[i - 1] + 1; j < x[i]; j++) {
				q += min(abs(x[i - 1] - j), abs(x[i] - j));
				p.push_back(j);
			}
		}
	}
	for (int i = 1; i <= rs; i++) {
		if (p.size() == m) break;
		q += i;
		p.push_back(x[1] - i);
		if (p.size() == m) break;
		q += i;
		p.push_back(x[n] + i);
	}
	cout << q << endl;
	for (auto i : p) {
		cout << i << ' ';
	}
}


#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>

using namespace std;

vector<int>trees, persons;
queue<int> q;
int n, m;
unordered_map<int, bool> verify;

int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		int x;
		cin >> x;
		trees.push_back(x);
	}
	for (int i = 0; i < n; i++)
	{
		q.push(trees[i]);
		verify[trees[i]]=1;
	}
	int count = 0;

	while (count < m)
	{
		int x;
		x = q.front();
		q.pop();
		q.push(x - 1);
		q.push(x + 1);

		if (!verify[x])
		{
			count++;
			persons.push_back(x);
			verify[x] = 1;
		}
	}
	for (auto it : persons)
	{
		cout << it << " ";
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <utility>

using namespace std;

vector<int>trees;
vector<pair<int, int>>persons;
queue<pair<int,int>> q;
int n, m;
unordered_map<int, bool> verify;

int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		int x;
		cin >> x;
		trees.push_back(x);
	}
	for (int i = 0; i < n; i++)
	{
		q.push(make_pair(trees[i],0));
		verify[trees[i]]=1;
	}
	int count = 0;

	while (count < m)
	{

		auto x = q.front();
		q.pop();
		q.push(make_pair(x.first - 1, x.second+1));
		q.push(make_pair(x.first + 1, x.second+1));

		if (!verify[x.first])
		{
			count++;
			persons.push_back(x);
			verify[x.first] = 1;
		}
	}
	int sum=0;
	for (auto it : persons)
		sum += it.second;
	cout << sum << endl;
	for (auto it : persons)
	{
		cout << it.first << " ";
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <utility>

using namespace std;

vector<int>trees, persons;
queue<int> q;
int n, m;
map<int,int> verify;

int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		int x;
		cin >> x;
		trees.push_back(x);
	}
	for (int i = 0; i < n; i++)
	{
		q.push(trees[i]);
		verify[trees[i]]=0;
	}
	int sum = 0;
	while (persons.size()<m) {

		int curr = q.front();
		q.pop();
		if (verify[curr] != 0) {
			sum += verify[curr];
			persons.push_back(curr);
		}
		if (!verify.count(curr - 1)) {
			verify[curr - 1] = verify[curr] + 1;
			q.push(curr - 1);
		}
		if (!verify.count(curr + 1)) {
			verify[curr + 1] = verify[curr] + 1;
			q.push(curr + 1);
		}

	}

	cout << sum << endl;
	for (auto it : persons)
		cout << it<< " ";

	return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <map>

using namespace std;

vector<int>trees, persons;
queue<int> q;
int n, m;
map<int,int> distances;

int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		int x;
		cin >> x;
		trees.push_back(x);
	}
	for (int i = 0; i < n; i++)
	{
		q.push(trees[i]);
		distances[trees[i]]=0;
	}

	long long  sum = 0;
	while (persons.size()<m) {

		int curr = q.front();
		q.pop();

		if (distances[curr] != 0) {
			sum += distances[curr];
			persons.push_back(curr);
		}
		if (!distances.count(curr - 1)) {
			distances[curr - 1] = distances[curr] + 1;
			q.push(curr - 1);
		}
		if (!distances.count(curr + 1)) {
			distances[curr + 1] = distances[curr] + 1;
			q.push(curr + 1);
		}

	}

	cout << sum << endl;
	for (auto it : persons)
		cout << it<< " ";

	return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <utility>

using namespace std;

vector<int>trees, persons;
queue<int> q;
int n, m;
map<int,int> verify;

int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		int x;
		cin >> x;
		trees.push_back(x);
	}
	for (int i = 0; i < n; i++)
	{
		q.push(trees[i]);
		verify[trees[i]]=0;
	}
	long long  sum = 0;
	while (persons.size()<m) {

		int curr = q.front();
		q.pop();
		if (verify[curr] != 0) {
			sum += verify[curr];
			persons.push_back(curr);
		}
		if (!verify.count(curr - 1)) {
			verify[curr - 1] = verify[curr] + 1;
			q.push(curr - 1);
		}
		if (!verify.count(curr + 1)) {
			verify[curr + 1] = verify[curr] + 1;
			q.push(curr + 1);
		}

	}

	cout << sum << endl;
	for (auto it : persons)
		cout << it<< " ";

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <queue>
#include <fstream>
#include <string>
#include <math.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> T;

const ll mod = 1000000007;
const ll INF = 100000010000;

ll n, m;
ll x[200010];
vector<ll> y;
ll ans;

ll gcd(ll x, ll y)
{
	if (y == 0) return x;
	return gcd(y, x % y);
}

ll pow_mod(ll x, ll y)
{
	if (y == 0) return 1;
	if (y % 2 == 1)
	{
		return x * pow_mod(x, y - 1) % mod;
	}
	else
	{
		ll half = pow_mod(x, y / 2);
		return half * half % mod;
	}
}

ll div_mod(ll x)
{
	return pow_mod(x, mod - 2);
}

void init()
{
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		cin >> x[i];
	}
	sort(x, x + n);
}

void output()
{
	cout << ans << endl;
	for (int i = 0; i < y.size(); i++)
	{
		cout << y[i];
		if (i == y.size() - 1)
		{
			cout << endl;
		}
		else
		{
			cout << ' ';
		}
	}
}

void solve()
{
	vector<P> dif;

	for (int i = 1; i < n; i++)
	{
		if (x[i] - x[i - 1] == 1) continue;
		dif.push_back(P(x[i] - x[i - 1], i));
	}
	dif.push_back(P(INF, 0));
	dif.push_back(P(INF, n));
	sort(dif.begin(), dif.end());
	reverse(dif.begin(), dif.end());
	ll cnt = 0;
	for (int i = 1; ; i++)
	{
		for (int j = 0; j < dif.size(); j++)
		{
			if (dif[j].first / 2 < i)
			{
				break;
			}
			if (dif[j].first % 2 == 0 && dif[j].first / 2 == i)
			{
				ans += i;
				if (dif[j].second == n)
				{
					y.push_back(x[n - 1] + i);
				}
				else
				{
					y.push_back(x[dif[j].second] - i);
				}
				if (y.size() == m) return;
			}
			else
			{
				if (dif[j].second == n)
				{
					ans += i;
					y.push_back(x[n - 1] + i);
					if (y.size() == m) return;
				}
				else if (dif[j].second == 0)
				{
					ans += i;
					y.push_back(x[0] - i);
					if (y.size() == m) return;
				}
				else
				{
					ans += i;
					y.push_back(x[dif[j].second - 1] + i);
					if (y.size() == m) return;
					ans += i;
					y.push_back(x[dif[j].second] - i);
					if (y.size() == m) return;
				}
			}
		}
	}
}

int main()
{
	init();

	solve();

	output();
	ll N;
	cin >> N;
}


#include "cstdio"
#include "cstring"
#include "queue"
#include "map"
#include "algorithm"
#include "vector"
#define ll long long
const int maxn = 2e5+20;
ll x[maxn];
std::map< ll ,bool>mp;
std::queue<ll>q;
std::vector<ll>vec;

int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
		scanf("%lld",x+i);//,q.push(x[i]);
	std::sort(x,x+n);
	for(int i=0;i<n;i++)
		q.push(x[i]),mp[x[i]]=1;
	int cnt=0;
	ll dist=1;
	ll ans=0,end=x[n-1];
	while(!q.empty())
	{
		ll u=q.front();
		q.pop();
		if(!mp.count(u+1))
			mp[u+1]=1,q.push(u+1),cnt++,vec.push_back(u+1),ans+=dist;
		if(cnt==m)
			break;
		if(!mp.count(u-1))
			mp[u-1]=1,q.push(u-1),cnt++,ans+=dist,vec.push_back(u-1);
		if(cnt==m)
			break;
		if(u==end)
			end++,dist++;
	}
	printf("%lld\n",ans);
	for(int i=0;i<vec.size();i++)
		printf("%lld ",vec[i]);

}


#include "cstdio"
#include "cstring"
#include "queue"
#include "map"
#include "algorithm"
#include "vector"
#define ll long long
const int maxn = 2e5+20;
ll x[maxn];
std::map<ll,bool>mp;
std::queue<ll>q;
std::vector<ll>vec;

int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
		scanf("%lld",x+i);//,q.push(x[i]);
	std::sort(x,x+n);
	for(int i=0;i<n;i++)
		q.push(x[i]),mp[x[i]]=1;
	int cnt=0;
	ll dist=1;
	ll ans=0,end=x[n-1];
	while(!q.empty())
	{
		ll u=q.front();
		q.pop();
		if(mp[u-1]!=1)
			mp[u-1]=1,q.push(u-1),m--,ans+=dist,vec.push_back(u-1);
		if(!m)
			break;
		if(mp[u+1]!=1)
			mp[u+1]=1,q.push(u+1),m--,vec.push_back(u+1),ans+=dist;
		if(!m)
			break;
		if(u==end)
			end++,dist++;
	}
	printf("%lld\n",ans);
	for(int i=0;i<vec.size();i++)
		printf("%lld ",vec[i]);

}


#include "cstdio"
#include "cstring"
#include "queue"
#include "map"
#include "algorithm"
#include "vector"
#define ll long long
const int maxn = 2e5+20;
ll x[maxn];
std::map<ll,bool>mp;
std::queue<ll>q;
std::vector<ll>vec;

int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
		scanf("%lld",x+i);//,q.push(x[i]);
	std::sort(x,x+n);
	for(int i=0;i<n;i++)
		q.push(x[i]),mp[x[i]]=1;
	int cnt=0;
	ll dist=1;
	ll ans=0,end=x[n-1];
	while(!q.empty())
	{
		ll u=q.front();
		q.pop();
		if(mp[u+1]!=1)
			mp[u+1]=1,q.push(u+1),m--,vec.push_back(u+1),ans+=dist;
		if(!m)
			break;
		if(mp[u-1]!=1)
			mp[u-1]=1,q.push(u-1),m--,ans+=dist,vec.push_back(u-1);
		if(!m)
			break;
		if(u==end)
			end++,dist++;
	}
	printf("%lld\n",ans);
	for(int i=0;i<vec.size();i++)
		printf("%lld ",vec[i]);

}


#include "cstdio"
#include "cstring"
#include "queue"
#include "map"
#include "algorithm"
#include "vector"
#define ll long long
const int maxn = 2e5+20;
ll x[maxn];
std::map< ll ,bool>mp;
std::queue<ll>q;
std::vector<ll>vec;

int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
		scanf("%lld",x+i);//,q.push(x[i]);
	std::sort(x,x+n);
	for(int i=0;i<n;i++)
		q.push(x[i]),mp[x[i]]=1;
	int cnt=0;
	ll dist=1;
	ll ans=0,end=x[n-1];
	while(!q.empty())
	{
		ll u=q.front();
		q.pop();
		if(!mp.count(u+1))
			mp[u+1]=1,q.push(u+1),cnt++,vec.push_back(u+1),ans+=dist;
		if(cnt==m)
			break;
		if(!mp.count(u-1))
			mp[u-1]=1,q.push(u-1),cnt++,ans+=dist,vec.push_back(u-1);
		if(cnt==m)
			break;
		if(u==end)
			end++,dist++;
	}
	printf("%lld\n",ans);
	for(int i=0;i<vec.size();i++)
		printf("%lld%s",vec[i],i==vec.size()-1?"\n":" ");

}


n,m=map(int,input().split())
arr=sorted(list(map(int,input().split())))
dist=[]
for _ in range(m//2+10):
	dist.append([])
for i in range(n-1):
	c=min(arr[i+1]-arr[i]-1,(n+m-1)//n)
	for k in range(c//2+c%2):
		dist[k+1].append(arr[i]+1+k)
	for k in range(c//2):
		dist[k+1].append(arr[i+1]-1-k)
	# for j in range(arr[i]+1,min((arr[i+1]-arr[i])//2,max(m-2*n)+1+arr[i]):
		# dist[j-arr[i]].append(j)
	# for j in range(arr[i+1]-min((arr[i+1]-arr[i])//2,m//2),arr[i+1]):
		# dist[arr[i+1]-j].append(j)
# print(dist)
for i in range(arr[0]-1,arr[0]-1-m//2-m%2-2,-1):
	dist[arr[0]-i].append(i)
for i in range(arr[-1]+1,arr[-1]+1+m//2+m%2+2):
	dist[i-arr[-1]].append(i)
i=1
ans=[]
v=0
#print(dist)
while(m):
	l=len(dist[i])
	if l<=m:
		m-=l
		v+=l*i
		ans+=dist[i]
	else:
		ans+=dist[i][:m]
		v+=m*i
		m=0
	i+=1
print(v)
print(*ans)


n,m=map(int,input().split())
arr=sorted(list(map(int,input().split())))
dist=[]
for _ in range(m//2+10):
	dist.append([])
for i in range(n-1):
	c=min(arr[i+1]-arr[i]-1,2*(200000+n-1)//n)
	# for k in range(c//2+c%2):
	# 	dist[k+1].append(arr[i]+1+k)
	# for k in range(c//2):
	# 	dist[k+1].append(arr[i+1]-1-k)
	for j in range(arr[i]+1,c//2+c%2+1+arr[i]):
		dist[j-arr[i]].append(j)
	for j in range(arr[i+1]-1,arr[i+1]-1-c//2,-1):
		dist[arr[i+1]-j].append(j)
# print(dist)
for i in range(arr[0]-1,arr[0]-1-m//2-m%2-2,-1):
	dist[arr[0]-i].append(i)
for i in range(arr[-1]+1,arr[-1]+1+m//2+m%2+2):
	dist[i-arr[-1]].append(i)
i=1
ans=[]
v=0
#print(dist)
while(m):
	l=len(dist[i])
	if l<=m:
		m-=l
		v+=l*i
		ans+=dist[i]
	else:
		ans+=dist[i][:m]
		v+=m*i
		m=0
	i+=1
print(v)
print(*ans)


n,m=map(int,input().split())
arr=sorted(list(map(int,input().split())))
dist=[]
for _ in range(m//2+10):
	dist.append([])
for i in range(n-1):
	c=min(arr[i+1]-arr[i]-1,(n+m-1)//n)
	for k in range(c//2+c%2):
		dist[k+1].append(arr[i]+1+k)
	for k in range(c//2):
		dist[k+1].append(arr[i+1]-1-k)
	# for j in range(arr[i]+1,min((arr[i+1]-arr[i])//2,max(m-2*n)+1+arr[i]):
		# dist[j-arr[i]].append(j)
	# for j in range(arr[i+1]-min((arr[i+1]-arr[i])//2,m//2),arr[i+1]):
		# dist[arr[i+1]-j].append(j)
print(dist)
for i in range(arr[0]-1,arr[0]-1-m//2-m%2-2,-1):
	dist[arr[0]-i].append(i)
for i in range(arr[-1]+1,arr[-1]+1+m//2+m%2+2):
	dist[i-arr[-1]].append(i)
i=1
ans=[]
v=0
#print(dist)
while(m):
	l=len(dist[i])
	if l<=m:
		m-=l
		v+=l*i
		ans+=dist[i]
	else:
		ans+=dist[i][:m]
		v+=m*i
		m=0
	i+=1
print(v)
print(*ans)


n,m=map(int,input().split())
arr=sorted(list(map(int,input().split())))
dist=[]
for _ in range(m//2+10):
	dist.append([])
for i in range(n-1):
	for j in range(arr[i]+1,arr[i+1]):
		z=min(j-arr[i],arr[i+1]-j)
		dist[z].append(j)
for i in range(arr[0]-1,arr[0]-1-m//2-m%2-2,-1):
	dist[arr[0]-i].append(i)
for i in range(arr[-1]+1,arr[-1]+1+m//2+m%2+2):
	dist[i-arr[-1]].append(i)
i=1
ans=[]
v=0
#print(dist)
while(m):
	l=len(dist[i])
	if l<=m:
		m-=l
		v+=l*i
		ans+=dist[i]
	else:
		ans+=dist[i][:m]
		v+=m*i
		m=0
	i+=1
print(v)
print(*ans)


n,m=map(int,input().split())
arr=sorted(list(map(int,input().split())))
dist=[]
for _ in range(m//2+10):
	dist.append([])
for i in range(n-1):
	c=min(arr[i+1]-arr[i]-1,(200000+n-1)//n)
	for k in range(c//2+c%2):
		dist[k+1].append(arr[i]+1+k)
	for k in range(c//2):
		dist[k+1].append(arr[i+1]-1-k)
	# for j in range(arr[i]+1,min((arr[i+1]-arr[i])//2,max(m-2*n)+1+arr[i]):
		# dist[j-arr[i]].append(j)
	# for j in range(arr[i+1]-min((arr[i+1]-arr[i])//2,m//2),arr[i+1]):
		# dist[arr[i+1]-j].append(j)
# print(dist)
for i in range(arr[0]-1,arr[0]-1-m//2-m%2-2,-1):
	dist[arr[0]-i].append(i)
for i in range(arr[-1]+1,arr[-1]+1+m//2+m%2+2):
	dist[i-arr[-1]].append(i)
i=1
ans=[]
v=0
#print(dist)
while(m):
	l=len(dist[i])
	if l<=m:
		m-=l
		v+=l*i
		ans+=dist[i]
	else:
		ans+=dist[i][:m]
		v+=m*i
		m=0
	i+=1
print(v)
print(*ans)


n,m=map(int,input().split())
arr=sorted(list(map(int,input().split())))
dist=[]
for _ in range(m//2+10):
	dist.append([])
for i in range(n-1):
	c=min(arr[i+1]-arr[i]-1,(200000+n-1)//n)
	# for k in range(c//2+c%2):
	# 	dist[k+1].append(arr[i]+1+k)
	# for k in range(c//2):
	# 	dist[k+1].append(arr[i+1]-1-k)
	for j in range(arr[i]+1,c//2+c%2+1+arr[i]):
		dist[j-arr[i]].append(j)
	for j in range(arr[i+1]-1,arr[i+1]-1-c//2,-1):
		dist[arr[i+1]-j].append(j)
# print(dist)
for i in range(arr[0]-1,arr[0]-1-m//2-m%2-2,-1):
	dist[arr[0]-i].append(i)
for i in range(arr[-1]+1,arr[-1]+1+m//2+m%2+2):
	dist[i-arr[-1]].append(i)
i=1
ans=[]
v=0
#print(dist)
while(m):
	l=len(dist[i])
	if l<=m:
		m-=l
		v+=l*i
		ans+=dist[i]
	else:
		ans+=dist[i][:m]
		v+=m*i
		m=0
	i+=1
print(v)
print(*ans)


n,m=map(int,input().split())
a=list(map(int,input().split()))
d={}
q=[]
for c in a:
	d[c]=1
	q.append(c)
ans=[]
v=0
i=0
while(len(ans)<m):
	x=q[i]
	if x-1 not in d:
		v+=d[x]
		ans.append(x-1)
		q.append(x-1)
		d[x-1]=d[x]+1
	if len(ans)==m:
		break
	if x+1 not in d:
		v+=d[x]
		ans.append(x+1)
		q.append(x+1)
		d[x+1]=d[x]+1
	i+=1
print(v)
print(*ans)


n, y = map(int, input().split())
q = list(map(int, input().split()))
v, m, ans = [], {}, 0
for i in q: m[i] = 0
while len(v) < y:
    x = q[0]; del(q[0])
    if m[x]:
        ans += m[x]
        v.append(x)
    if(x + 1 not in m):
        m[x + 1] = m[x] + 1
        q.append(x + 1)
    if len(v) == y: break
    if(x - 1 not in m):
        m[x - 1] = m[x] + 1
        q.append(x - 1)
print(ans)
for i in v: print(i, end = ' ')


#! /usr/bin/python3

n, y = map(int, input().split())
q = list(map(int, input().split()))
v, m, ans, j = [], {}, 0, 0
for i in q: m[i] = 0
while len(v) != y:
    x = q[j];
    if m[x]:
        ans += m[x]
        v.append(x)
    if(x + 1 not in m):
        m[x + 1] = m[x] + 1
        q.append(x + 1)
    if(x - 1 not in m):
        m[x - 1] = m[x] + 1
        q.append(x - 1)
    j += 1
print(ans)
for i in v: print(i, end = ' ')


n, y = map(int, input().split())
q = list(map(int, input().split()))
v, m, ans = [], {}, 0
for i in q: m[i] = 0
while len(v) != y:
    x = q[0]; del(q[0])
    if m[x]:
        ans += m[x]
        v.append(x)
    if(x + 1 not in m):
        m[x + 1] = m[x] + 1
        q.append(x + 1)
    if(x - 1 not in m):
        m[x - 1] = m[x] + 1
        q.append(x - 1)
print(ans)
for i in v: print(i, end = ' ')


n, y = map(int, input().split())
q = list(map(int, input().split()))
v, m, ans = [], {}, 0
for i in q: m[i] = 0
while len(v) != y:
    x = q[0]; q.pop(0)
    if m[x]:
        ans += m[x]
        v.append(x)
    if(x + 1 not in m):
        m[x + 1] = m[x] + 1
        q.append(x + 1)
    if(x - 1 not in m):
        m[x - 1] = m[x] + 1
        q.append(x - 1)
print(ans)
for i in v: print(i, end = ' ')


#! /usr/bin/python3

n, y = map(int, input().split())
q = list(map(int, input().split()))
v, m, ans = [], {}, 0
for i in q: m[i] = 0
while len(v) != y:
    x = q[0]; del(q[0])
    if(x + 1 not in m):
        m[x + 1] = m[x] + 1
        ans += m[x + 1]
        v.append(x + 1)
        q.append(x + 1)
    if(x - 1 not in m):
        m[x - 1] = m[x] + 1
        ans += m[x - 1]
        v.append(x - 1)
        q.append(x - 1)
print(ans)
for i in v: print(i, end = ' ')


def inp(dtype=str, strip=True):
    s = input()
    res = [dtype(p) for p in s.split()]
    res = res[0] if len(res) == 1 and strip else res
    return res

def problem1():
    t = inp(int)
    for _ in range(t):
        h, m = inp(int)
        print((23 - h) * 60 + (60 - m))

def problem2():
    t = inp(int)
    for _ in range(t):
        n, k = inp(int)
        res = (n // k) * k + min(n % k, k // 2)
        print(res)

def problem3():
    n = inp(int)
    f = inp(int, strip=False)
    f = [ch - 1 for ch in f]
    g = [-1 for _ in range(n)]
    for i, ch in enumerate(f):
        if ch >= 0:
            g[ch] = i

    gave = set([i for i in range(n) if f[i] >= 0 and g[i] == -1])
    received = set([i for i in range(n) if f[i] == -1 and g[i] >= 0])
    neither = set([i for i in range(n) if f[i] == -1 and g[i] == -1])

    # give a gift to anyone who has not yet given one
    # and has not yet received one

    while len(neither):
        # i is giving to j
        if len(received):
            i = received.pop()
        else:
            i = neither.pop()
            gave.add(i)
        j = neither.pop()
        received.add(j)
        f[i] = j

    while len(received):
        i = received.pop()
        j = gave.pop()
        f[i] = j

    f = [ch + 1 for ch in f]
    print(' '.join([str(ch) for ch in f]))

def problem4():
    max_m = 10 ** 6
    n, m = inp(int)
    x = inp(int, strip=False)

    x = sorted(x)

    empty = [(0, 0) for _ in range(n + 1)]
    for i in range(1, n):
        empty[i] = (x[i] - x[i - 1] - 1, i)
    empty[0] = (max_m, 0)
    empty[-1] = (max_m, n)

    empty = sorted(empty, key=lambda tup: tup[0])

    cur_dist = 1
    start = 0
    while empty[start][0] == 0:
        start += 1
    j = 0
    y = [0 for _ in range(m)]
    p = start
    res = 0

    while j < m:
        # print(x, empty, y)

        num, i = empty[p]
        res += cur_dist
        if i == 0:
            # to the left of tree #0
            num -= 1
            y[j] = x[i] - cur_dist
            j += 1
        elif i == n:
            # to the right of tree #n-1
            num -= 1
            y[j] = x[i - 1] + cur_dist
            j += 1
        else:
            # between trees i - 1 and i
            num -= 1
            y[j] = x[i - 1] + cur_dist
            j += 1
            if j < m and num > 0:
                num -= 1
                y[j] = x[i] - cur_dist
                j += 1
                res += cur_dist

        if num == 0:
            start += 1
        empty[p] = (num, i)

        p += 1
        if p == n + 1:
            cur_dist += 1
            p = start

    print(res)
    print(' '.join([str(k) for k in y]))

def problem5():
    pass

if __name__ == '__main__':
    # problem1()
    # problem2()
    # problem3()
    problem4()
    # problem5()


po, men = map(int, input().split())
a = {int(i) for i in input().split()}
r = {*a}
z = {*a}
men += len(r)
k = 1
s = 0
while len(r) < men:
    b = set()
    for i in a:
        for p in -1, 1:
            if i + p*k not in r:
                if len(r) < men:
                    s += abs(p*k)
                    r.add(i + p*k)
                    b.add(i)
                    if len(r) == men:
                        break
        if len(r) == men:
            break
    k += 1
    a = b
print(s)
print(*(r-z))


def christmas(n,m,A):
    d={}
    q=[]
    for i in A:
        d[i]=0
        q.append(i)
    mini=0
    res=[]
    i=0
    while len(res)<m:
        k=q[i]
        if k-1 not in d:
            d[k-1]=d[k]+1
            mini+=d[k-1]
            q.append(k-1)
            res.append(k-1)
        if len(res)==m:
            break
        if k+1 not in d:
            d[k+1]=d[k]+1
            mini+=d[k+1]
            q.append(k+1)
            res.append(k+1)
        i+=1

    print(mini)
    print(*res)
n,m=map(int,input().split())
arr=list(map(int,input().split()))
christmas(n,m,arr)


from collections import defaultdict
def christmas(n,m,A):
    d=defaultdict(int)
    q=[]
    for i in A:
        d[i]=0
        q.append(i)
    mini=0
    res=[]
    i=0
    while len(res)<m:
        k=q[i]
        if k-1 not in d:
            d[k-1]=d[k]+1
            mini+=d[k-1]
            q.append(k-1)
            res.append(k-1)
        if len(res)==m:
            break
        if k+1 not in d:
            d[k+1]=d[k]+1
            mini+=d[k+1]
            q.append(k+1)
            res.append(k+1)
        i+=1

    print(mini)
    print(*res)
n,m=map(int,input().split())
arr=list(map(int,input().split()))
christmas(n,m,arr)


def christmas(n,m,A):
    d={}
    q=[]
    for i in A:
        d[i]=0
        q.append(i)
    mini=0
    res=[]
    while len(res)<m:
        k=q.pop(0)
        if k+1 not in d:
            d[k+1]=d[k]+1
            mini+=d[k+1]
            q.append(k+1)
            res.append(k+1)
        if len(res)==m:
            break
        if k-1 not in d:
            d[k-1]=d[k]+1
            mini+=d[k-1]
            q.append(k-1)
            res.append(k-1)

    print(mini)
    print(*res)
n,m=map(int,input().split())
arr=list(map(int,input().split()))
christmas(n,m,arr)


from collections import defaultdict
def christmas(n,m,A):
    d=defaultdict(int)
    q=[]
    for i in A:
        d[i]=0
        q.append(i)
    mini=0
    res=[]
    while len(res)<m:
        k=q.pop(0)
        if k-1 not in d:
            d[k-1]=d[k]+1
            mini+=d[k-1]
            q.append(k-1)
            res.append(k-1)
        if len(res)==m:
            break
        if k+1 not in d:
            d[k+1]=d[k]+1
            mini+=d[k+1]
            q.append(k+1)
            res.append(k+1)

    print(mini)
    print(*res)
n,m=map(int,input().split())
arr=list(map(int,input().split()))
christmas(n,m,arr)


def christmas(n,m,A):
    d={}
    q=[]
    for i in A:
        d[i]=0
        q.append(i)
    mini=0
    res=[]
    while len(res)<m:
        k=q.pop(0)
        if k-1 not in d:
            d[k-1]=d[k]+1
            mini+=d[k-1]
            q.append(k-1)
            res.append(k-1)
        if len(res)==m:
            break
        if k+1 not in d:
            d[k+1]=d[k]+1
            mini+=d[k+1]
            q.append(k+1)
            res.append(k+1)

    print(mini)
    print(*res)
n,m=map(int,input().split())
arr=list(map(int,input().split()))
christmas(n,m,arr)


n,m = map(int,input().split())

queue = list(map(int,input().split()))

hash = {}

for i in queue:
    hash[i] = 1

ans = []
yo = 0

d = 0
while(len(ans)<m):

    z = queue.pop()
    if yo == m:
        break
    if z-1 not in hash:
        d+=hash[z]
        queue.append(z-1)
        yo+=1
        ans.append(z-1)

        hash[z-1] = hash[z]+1

    if z+1 not in hash:
        d+=hash[z]
        queue.append(z+1)
        yo+=1
        ans.append(z+1)

        hash[z+1] = hash[z]+1

print(d)
print(*ans)


from sys import stdin
from collections import deque
mod = 10**9 + 7
import sys
# def rl():
#     return [int(w) for w in stdin.readline().split()]
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt,factorial,gcd,log2,inf,ceil
# map(int,input().split())
# # l = list(map(int,input().split()))
# from itertools import permutations
import heapq
# input = lambda: sys.stdin.readline().rstrip()
input = lambda : sys.stdin.readline().rstrip()

n,m = map(int,input().split())

queue = list(map(int,input().split()))

hash = [False]*(2000005)
for i in queue:
    hash[i] = True
ans = []
yo = 0
dis = [0]*(2000005)
d = 0
while queue!=[]:

    z = queue.pop(0)
    if yo == m:
        break
    if hash[z-1] == False:
        queue.append(z-1)
        yo+=1
        hash[z-1] = True
        ans.append(z-1)
        dis[z-1]+=dis[z]+1
        d+=dis[z]+1
    if hash[z+1] == False:
        queue.append(z+1)
        yo+=1
        hash[z+1] = True
        ans.append(z+1)
        dis[z+1]+=dis[z]+1
        d+=dis[z]+1
print(d)
print(*ans)


n,m = map(int,input().split())

queue = list(map(int,input().split()))

hash = {}

for i in queue:
    hash[i] = 1

ans = []
yo = 0

d = 0
while queue!=[]:

    z = queue.pop(0)
    if yo == m:
        break
    if z-1 not in hash:
        d+=hash[z]
        queue.append(z-1)
        yo+=1
        ans.append(z-1)
        hash[z-1] = 0
        hash[z-1] = hash[z]+1

    if z+1 not in hash:
        d+=hash[z]
        queue.append(z+1)
        yo+=1
        ans.append(z+1)
        hash[z+1] = 0
        hash[z+1] = hash[z]+1

print(d)
print(*ans)


from sys import stdin
from collections import deque
mod = 10**9 + 7
import sys
# def rl():
#     return [int(w) for w in stdin.readline().split()]
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt,factorial,gcd,log2,inf,ceil
# map(int,input().split())
# # l = list(map(int,input().split()))
# from itertools import permutations
import heapq
# input = lambda: sys.stdin.readline().rstrip()
input = lambda : sys.stdin.readline().rstrip()

n,m = map(int,input().split())

queue = list(map(int,input().split()))

hash = {}

for i in queue:
    hash[i] = 1

ans = []
yo = 0

d = 0
while queue!=[]:

    z = queue.pop(0)
    if yo == m:
        break
    if z-1 not in hash:
        d+=hash[z]
        queue.append(z-1)
        yo+=1
        ans.append(z-1)
        hash[z-1] = 0
        hash[z-1] = hash[z]+1

    if z+1 not in hash:
        d+=hash[z]
        queue.append(z+1)
        yo+=1
        ans.append(z+1)
        hash[z+1] = 0
        hash[z+1] = hash[z]+1

print(d)
print(*ans)


from sys import stdin
from collections import deque
mod = 10**9 + 7
import sys
# def rl():
#     return [int(w) for w in stdin.readline().split()]
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt,factorial,gcd,log2,inf,ceil
# map(int,input().split())
# # l = list(map(int,input().split()))
# from itertools import permutations
import heapq
# input = lambda: sys.stdin.readline().rstrip()
input = lambda : sys.stdin.readline().rstrip()

n,m = map(int,input().split())

queue = list(map(int,input().split()))

hash = {}
dis = {}
for i in queue:
    hash[i] = True
    dis[i] = 0
ans = []
yo = 0

d = 0
while queue!=[]:

    z = queue.pop(0)
    if yo == m:
        break
    if z-1 not in hash:
        queue.append(z-1)
        yo+=1
        hash[z-1] = True
        ans.append(z-1)
        dis[z-1] = 0
        dis[z-1]+=dis[z]+1
        d+=dis[z]+1
    if z+1 not in hash:
        queue.append(z+1)
        yo+=1
        hash[z+1] = True
        ans.append(z+1)
        dis[z+1] = 0
        dis[z+1]+=dis[z]+1
        d+=dis[z]+1
print(d)
print(*ans)


n,m=map(int,input().split())
a=list(map(int,input().split()))
d={}
q=[]
for c in a:
	d[c]=1
	q.append(c)
ans=[]
v=0
i=0
while(len(ans)<m):
	x=q[i]
	if x-1 not in d:
		v+=d[x]
		ans.append(x-1)
		q.append(x-1)
		d[x-1]=d[x]+1
	if len(ans)==m:
		break
	if x+1 not in d:
		v+=d[x]
		ans.append(x+1)
		q.append(x+1)
		d[x+1]=d[x]+1
	i+=1
print(v)
print(*ans)


n,m = map(int,input().split())

queue = list(map(int,input().split()))

hash = {}

for i in queue:
    hash[i] = 1

ans = []
yo = 0

d = 0
while(len(ans)<m):

    z = queue.pop(0)
    if yo == m:
        break
    if z-1 not in hash:
        d+=hash[z]
        queue.append(z-1)
        yo+=1
        ans.append(z-1)
        hash[z-1] = 0
        hash[z-1] = hash[z]+1

    if z+1 not in hash:
        d+=hash[z]
        queue.append(z+1)
        yo+=1
        ans.append(z+1)
        hash[z+1] = 0
        hash[z+1] = hash[z]+1

print(d)
print(*ans)


import sys
from math import ceil, floor

n, m, *x = map(int, sys.stdin.read().split())
x = set(x)

def main(m):
    y = set()
    ave = sum(x) / n
    if ave == ave // 1:
        if not ave in x:
            y.add(int(ave))
            m -= 1

    l = ceil(ave) - 1
    r = floor(ave) + 1

    for _ in range(m):
        while (l in x) or (l in y):
            l -= 1
        while (r in x) or (r in y):
            r += 1
        dl = ave - l
        dr = r - ave
        if dl >= dr:
            y.add(r)
        else:
            y.add(l)

    return y

if __name__ == '__main__':
    ans = main(m)
    print(*ans, sep=' ')


import sys
from math import ceil, floor
from bisect import bisect_left as bi_l

n, m, *x = map(int, sys.stdin.read().split())
x = set(x)

def main(x, m):
    y = set()
    ave = sum(x) / n
    if ave == ave // 1:
        if not ave in x:
            y.add(int(ave))
            m -= 1

    l = ceil(ave) - 1
    r = floor(ave) + 1

    for _ in range(m):
        while (l in x) or (l in y):
            l -= 1
        while (r in x) or (r in y):
            r += 1
        dl = ave - l
        dr = r - ave
        if dl >= dr:
            y.add(r)
        else:
            y.add(l)

    x = sorted(x)
    res = 0
    for v in y:
        i = bi_l(x, v)
        if i == 0:
            d = abs(x[0] - v)
        elif i == n:
            d = abs(x[n-1] - v)
        else:
            d = min(abs(x[i] - v), abs(x[i-1] - v))
        res += d
    print(res)
    return y

if __name__ == '__main__':
    ans = main(x, m)
    print(*ans, sep=' ')


import sys
from heapq import heappush, heappop

n, m, *x = map(int, sys.stdin.read().split())
x.sort()

def main():
    filled = set(x)

    dist = []
    for v in x:
        if not v - 1 in filled:
            heappush(dist, (1, v, 'l'))
        if not v + 1 in filled:
            heappush(dist, (1, v, 'r'))

    res = []
    total = 0
    cnt = 0
    while cnt < m:
        d, v, lr = heappop(dist)
        if lr == 'l':
            u = v - d
        else:
            u = v + d

        if not u in filled:
            total += d
            res.append(u)
            filled.add(u)
            cnt += 1
            if lr == 'l':
                if not u - 1 in filled:
                    heappush(dist, (d+1, v, 'l'))
            else:
                if not u + 1 in filled:
                    heappush(dist, (d+1, v, 'r'))

    print(total)
    return res

if __name__ == '__main__':
    ans = main()
    print(*ans, sep=' ')


import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())

def solve():
	n, m = mints()
	x = list(mints())
	x.sort()
	q = []
	for i in range(n-1):
		s = x[i]
		d = x[i+1] - s
		if d > 3:
			q.append((d//2, s+1, 1))
			q.append(((d-1)//2,s+d-1, -1))
		elif d == 2:
			q.append((1,s+1,1))
	q.append((int(1e9),x[0]-1,-1))
	q.append((int(1e9),x[-1]+1,1))
	q.sort(reverse=True)
	c = 0
	j = 0
	ss = 0
	y = [0]*m
	while c < m:
		for e,s,d in q:
			if e <= j:
				break
			else:
				y[c] = s + j*d
				#print(y)
				ss += j+1
				c += 1
				if c == m:
					break
		j += 1
	print(ss)
	print(' '.join(map(str, y)))

#for i in range(mint()):
solve()


import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())

def solve():
	n, m = mints()
	q = list(mints())
	w = dict()
	for i in q:
		w[i] = 0
	ql = 0
	c = 0
	ss = 0
	y = []
	while ql < len(q):
		p = q[ql]
		ql += 1
		x = p - 1
		if x not in w:
			d = w[p] + 1
			y.append(x)
			q.append(x)
			ss += d
			w[x] = d
			c += 1
			if c == m:
				break
		x = p + 1
		if x not in w:
			d = w[p] + 1
			q.append(x)
			y.append(x)
			ss += d
			w[x] = d
			c += 1
			if c == m:
				break
	print(ss)
	print(' '.join(map(str, y)))

#for i in range(mint()):
solve()


import math

if __name__ == "__main__":
    n, m = list(map(int, input().split(' ')))
    a = list(map(int, input().split(' ')))
    ax = []
    # code your algorithm here
    otrs = sorted([[nc, nc, nc] for nc in a], key=lambda x: x[1])
    mp = 0
    r = 0
    while mp < m:
        otrs_r = []
        for i in range(len(otrs)):
            if mp >= m:
                break
            used = False
            if i == 0 or otrs[i - 1][2] < otrs[i][0] - 1:
                otrs[i][0] -= 1
                r += abs(otrs[i][1] - otrs[i][0])
                mp += 1
                ax.append(otrs[i][0])
                if mp == m:
                    break
                used = True
            if i == len(otrs) - 1 or otrs[i][2] + 1 < otrs[i + 1][0]:
                otrs[i][2] += 1
                r += abs(otrs[i][2] - otrs[i][1])
                mp += 1
                used = True
                ax.append(otrs[i][2])
                if mp == m:
                    break
            if used:
                otrs_r += [otrs[i]]
        otrs = otrs_r
    print(r)
    for i in range(m):
        print(ax[i], end=' ')
    print()


import math

if __name__ == "__main__":
    n, m = 200000, 200000  # list(map(int, input().split(' ')))
    a = list(range(200000))  # list(map(int, input().split(' ')))
    ax = []
    print('started function')
    # code your algorithm here
    otrs = sorted([[nc, nc, nc] for nc in a], key=lambda x: x[1])
    mp = 0
    r = 0
    while mp != m:
        otrs_r = []
        for i in range(len(otrs)):
            if mp >= m:
                break
            used = False
            if i == 0 or otrs[i - 1][2] < otrs[i][0] - 1:
                otrs[i][0] -= 1
                r += otrs[i][1] - otrs[i][0]
                mp += 1
                ax.append(otrs[i][0])
                used = True
            if i == len(otrs) - 1 or otrs[i][2] + 1 < otrs[i + 1][0]:
                otrs[i][2] += 1
                r += otrs[i][2] - otrs[i][1]
                mp += 1
                used = True
                ax.append(otrs[i][2])
            if used:
                otrs_r += [otrs[i]]
        otrs = otrs_r
    print(r)
    for i in ax:
        print(i, end=' ')
    print()


import math

if __name__ == "__main__":
    n, m = 200000, 200000  # list(map(int, input().split(' ')))
    a = list(range(200000))  # list(map(int, input().split(' ')))
    ax = []
    # code your algorithm here
    otrs = sorted([[nc, nc, nc] for nc in a], key=lambda x: x[1])
    mp = 0
    r = 0
    while mp != m:
        otrs_r = []
        for i in range(len(otrs)):
            if mp >= m:
                break
            used = False
            if i == 0 or otrs[i - 1][2] < otrs[i][0] - 1:
                otrs[i][0] -= 1
                r += otrs[i][1] - otrs[i][0]
                mp += 1
                ax.append(otrs[i][0])
                used = True
            if i == len(otrs) - 1 or otrs[i][2] + 1 < otrs[i + 1][0]:
                otrs[i][2] += 1
                r += otrs[i][2] - otrs[i][1]
                mp += 1
                used = True
                ax.append(otrs[i][2])
            if used:
                otrs_r += [otrs[i]]
        otrs = otrs_r
    print(r)
    for i in ax:
        print(i, end=' ')
    print()


import math

if __name__ == "__main__":
    n, m = list(map(int, input().split(' ')))
    a = list(map(int, input().split(' ')))
    ax = []
    # code your algorithm here
    otrs = sorted([[nc, nc, nc] for nc in a], key=lambda x: x[1])
    mp = 0
    r = 0
    while mp < m:
        otrs_r = []
        for i in range(len(otrs)):
            if mp >= m:
                break
            used = False
            if i == 0 or otrs[i - 1][2] < otrs[i][0] - 1:
                otrs[i][0] -= 1
                r += otrs[i][1] - otrs[i][0]
                mp += 1
                ax.append(otrs[i][0])
                if mp == m:
                    break
                used = True
            if i == len(otrs) - 1 or otrs[i][2] + 1 < otrs[i + 1][0]:
                otrs[i][2] += 1
                r += otrs[i][2] - otrs[i][1]
                mp += 1
                used = True
                ax.append(otrs[i][2])
                if mp == m:
                    break
            if used:
                otrs_r += [otrs[i]]
        otrs = otrs_r
    print(r)
    for i in range(m):
        print(ax[i], end=' ')
    print()


import math

if __name__ == "__main__":
    n, m = list(map(int, input().split(' ')))
    a = list(map(int, input().split(' ')))
    ax = []

    # code your algorithm here
    otrs = sorted([[nc, nc, nc] for nc in a], key=lambda x: x[1])
    mp = 0
    r = 0
    while mp != m:
        for i in range(len(otrs)):
            if mp >= m:
                break
            if i == 0 or otrs[i - 1][2] < otrs[i][0] - 1:
                otrs[i][0] -= 1
                r += otrs[i][1] - otrs[i][0]
                mp += 1
                ax.append(otrs[i][0])
            if i == n - 1 or otrs[i][2] + 1 < otrs[i + 1][0]:
                otrs[i][2] += 1
                r += otrs[i][2] - otrs[i][1]
                mp += 1
                ax.append(otrs[i][2])
    print(r)
    for i in ax:
        print(i, end=' ')
    print()


import math

if __name__ == "__main__":
    n, m = 200000, 200000  # list(map(int, input().split(' ')))
    a = list(range(200000))  # list(map(int, input().split(' ')))
    ax = []
    print('started function')
    # code your algorithm here
    otrs = sorted([[nc, nc, nc] for nc in a], key=lambda x: x[1])
    mp = 0
    r = 0
    while mp != m:
        print('here we go again ',mp)

        otrs_r = []
        for i in range(len(otrs)):
            if mp >= m:
                break
            used = False
            if i == 0 or otrs[i - 1][2] < otrs[i][0] - 1:
                otrs[i][0] -= 1
                r += otrs[i][1] - otrs[i][0]
                mp += 1
                ax.append(otrs[i][0])
                used = True
            if i == len(otrs) - 1 or otrs[i][2] + 1 < otrs[i + 1][0]:
                otrs[i][2] += 1
                r += otrs[i][2] - otrs[i][1]
                mp += 1
                used = True
                ax.append(otrs[i][2])
            if used:
                otrs_r += [otrs[i]]
        otrs = otrs_r
    print(r)
    for i in ax:
        print(i, end=' ')
    print()


import math

if __name__ == "__main__":
    n, m = list(map(int, input().split(' ')))
    a = list(map(int, input().split(' ')))
    ax = []
    # code your algorithm here
    otrs = sorted([[nc, nc, nc] for nc in a], key=lambda x: x[1])
    mp = 0
    r = 0
    while mp < m:
        otrs_r = []
        l_used = 0
        for i in range(len(otrs)):
            if mp >= m:
                break
            used = False
            if i == 0 or otrs[i - 1][2] < otrs[i][0] - 1:
                otrs[i][0] -= 1
                r += abs(otrs[i][1] - otrs[i][0])
                mp += 1
                ax.append(otrs[i][0])
                if mp == m:
                    break
                used = True
            if i == len(otrs) - 1 or otrs[i][2] + 1 < otrs[i + 1][0]:
                otrs[i][2] += 1
                r += abs(otrs[i][2] - otrs[i][1])
                mp += 1
                used = True
                ax.append(otrs[i][2])
                if mp == m:
                    break
            if used:
                otrs_r += [otrs[i]]
                l_used = i
            else:
                otrs[l_used][2] = otrs[i][2]

        otrs = otrs_r
    print(r)
    for i in range(m):
        print(ax[i], end=' ')
    print()


import math

if __name__ == "__main__":
    n, m = list(map(int, input().split(' ')))
    a = list(map(int, input().split(' ')))
    ax = []
    # code your algorithm here
    otrs = sorted([[nc, nc, nc] for nc in a], key=lambda x: x[1])
    mp = 0
    r = 0
    while mp != m:
        otrs_r = []
        for i in range(len(otrs)):
            if mp >= m:
                break
            used = False
            if i == 0 or otrs[i - 1][2] < otrs[i][0] - 1:
                otrs[i][0] -= 1
                r += otrs[i][1] - otrs[i][0]
                mp += 1
                ax.append(otrs[i][0])
                used = True
            if i == len(otrs) - 1 or otrs[i][2] + 1 < otrs[i + 1][0]:
                otrs[i][2] += 1
                r += otrs[i][2] - otrs[i][1]
                mp += 1
                used = True
                ax.append(otrs[i][2])
            if used:
                otrs_r += [otrs[i]]
        otrs = otrs_r
    print(r)
    for i in ax:
        print(i, end=' ')
    print()


import math

if __name__ == "__main__":
    n, m = list(map(int, input().split(' ')))
    a = list(map(int, input().split(' ')))
    ax = []
    # code your algorithm here
    otrs = sorted([[nc, nc, nc] for nc in a], key=lambda x: x[1])
    mp = 0
    r = 0
    while mp < m:
        otrs_r = []
        l_used = 0
        for i in range(len(otrs)):
            if mp >= m:
                break
            used = False
            if i == 0 or otrs[i - 1][2] < otrs[i][0] - 1:
                otrs[i][0] -= 1
                r += abs(otrs[i][1] - otrs[i][0])
                mp += 1
                ax.append(otrs[i][0])
                if mp == m:
                    break
                used = True
            if i == len(otrs) - 1 or otrs[i][2] + 1 < otrs[i + 1][0]:
                otrs[i][2] += 1
                r += abs(otrs[i][2] - otrs[i][1])
                mp += 1
                used = True
                ax.append(otrs[i][2])
                if mp == m:
                    break
            if used:
                otrs_r += [otrs[i]]
                l_used = i
            else:
                otrs[l_used][2] = otrs[i][0]

        otrs = otrs_r
    print(r)
    for i in range(m):
        print(ax[i], end=' ')
    print()


import math

if __name__ == "__main__":
    n, m = list(map(int, input().split(' ')))
    a = list(map(int, input().split(' ')))
    ax = []
    # code your algorithm here
    otrs = sorted([[nc, nc, nc] for nc in a], key=lambda x: x[1])
    mp = 0
    r = 0
    while mp != m:
        otrs_r = []
        for i in range(len(otrs)):
            if mp >= m:
                break
            used = False
            if i == 0 or otrs[i - 1][2] < otrs[i][0] - 1:
                otrs[i][0] -= 1
                r += otrs[i][1] - otrs[i][0]
                mp += 1
                if mp == m:
                    break
                ax.append(otrs[i][0])
                used = True
            if i == len(otrs) - 1 or otrs[i][2] + 1 < otrs[i + 1][0]:
                otrs[i][2] += 1
                r += otrs[i][2] - otrs[i][1]
                mp += 1
                if mp == m:
                    break
                used = True
                ax.append(otrs[i][2])
            if used:
                otrs_r += [otrs[i]]
        otrs = otrs_r
    print(r)
    for i in range(m):
        print(ax[i], end=' ')
    print()


from collections import defaultdict
n,m=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
d1=defaultdict(list)
d2=[]
result=[]
ans=0
for i in range(n):
    d1[l[i]]=0
    d2.append(l[i])
while(len(d2)!=0):
    if(len(result)==m):
        break
    cur=d2[0]
    d2.pop(0)

    if(d1[cur]!=0):
        ans+=d1[cur]
        result.append(cur)
    if cur-1 not in d1:
        d1[cur-1]=d1[cur]+1
        d2.append(cur-1)
    if cur+1 not in d2:
        d1[cur+1]=d1[cur]+1
        d2.append(cur+1)
print(ans)
for i in result:
    print(i,end=" ")


from collections import defaultdict
n,m=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
d1=defaultdict(lambda: "safal")
d2=[]
result=[]
ans=0
for i in range(n):
    d1[l[i]]=0
    d2.append(l[i])
t=0
while(len(d2)!=t+1):
    if(len(result)==m):
        break
    cur=d2[t]
    t+=1

    if(d1[cur]!=0 and d1[cur]!="safal"):
        ans+=d1[cur]
        result.append(cur)
    if(d1[cur-1]=="safal"):
        d1[cur-1]=d1[cur]+1
        d2.append(cur-1)
    if(d1[cur+1]=="safal"):
        d1[cur+1]=d1[cur]+1
        d2.append(cur+1)
print(ans)
for i in result:
    print(i,end=" ")


from collections import defaultdict
n,m=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
d1=defaultdict(lambda: "safal")
d2=[]
result=[]
ans=0
for i in range(n):
    d1[l[i]]=0
    d2.append(l[i])
while(len(d2)!=0):
    if(len(result)==m):
        break
    cur=d2[0]
    d2.pop(0)

    if(d1[cur]!=0 and d1[cur]!="safal"):
        ans+=d1[cur]
        result.append(cur)
    if(d1[cur-1]=="safal"):
        d1[cur-1]=d1[cur]+1
        d2.append(cur-1)
    if(d1[cur+1]=="safal"):
        d1[cur+1]=d1[cur]+1
        d2.append(cur+1)
print(ans)
for i in result:
    print(i,end=" ")


from collections import defaultdict
n,m=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
d1=defaultdict(lambda: "safal")
d2=[]
result=[]
ans=0
for i in range(n):
    d1[l[i]]=0
    d2.append(l[i])
t=0
while(len(d2)!=t):
    if(len(result)==m):
        break
    cur=d2[t]
    t+=1

    if(d1[cur]!=0 and d1[cur]!="safal"):
        ans+=d1[cur]
        result.append(cur)
    if(d1[cur-1]=="safal"):
        d1[cur-1]=d1[cur]+1
        d2.append(cur-1)
    if(d1[cur+1]=="safal"):
        d1[cur+1]=d1[cur]+1
        d2.append(cur+1)
print(ans)
for i in result:
    print(i,end=" ")


from bisect import bisect_left as bl, bisect_right as br, insort
import sys
import heapq
# from math import *
from collections import defaultdict as dd, deque
def data(): return sys.stdin.readline().strip()
def mdata(): return map(int, data().split())
#sys.setrecursionlimit(1000000)
mod = int(1e9 + 7)

n,m=mdata()
x=list(mdata())
d=dd(int)
for i in x:
    d[i]=1
s=set(x)
cnt=m
l=[]
ans=0
t=1
while cnt>0:
    s1=set()
    for i in s:
        if d[i+1]==0:
            s1.add(i+1)
            d[i+1]=1
            ans+=t
            cnt-=1
            l.append(i+1)
        if d[i-1]==0:
            s1.add(i-1)
            d[i-1]=1
            ans+=t
            cnt-=1
            l.append(i-1)
        if cnt==0:
            break
    t+=1
    s=s1
print(ans)
print(*l)