#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <climits>
#define LSOne(S)(S & (-S))
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
vi copia;
int N,W;
const ll BIG = 3e+14+10;
const int MAXN = 400005;
int state[MAXN];
int stateok[MAXN];
int O[MAXN];
int A[MAXN];
int B[MAXN];
int C[MAXN];
class FenwickTree{
	public:vi ft;
	FenwickTree(){ ft.assign(MAXN+1,0);}

	ll rsq(int b){
		ll sum = 0; for(;b;b-=LSOne(b)) sum+= ft[b]; return sum;}

	ll rsq(int a, int b){
		return rsq(b) - (a == 1 ? 0 : rsq(a-1));}

	void adjust(int k, int v){
		for(; k < (int)ft.size(); k+= LSOne(k)) ft[k] += v;}

	void clear() {
		ft.clear();
		ft.assign(MAXN+1,0);
	}
};

FenwickTree ok;
FenwickTree quanto;

void remove(int pos){
	ok.adjust(C[pos], -1 );
	quanto.adjust(C[pos], -B[O[C[pos]]] );
}

ll compute(int a){
	if(a==0) return 0;
	//~ cout << "Computing " << a << endl;
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << "i :" <<  quanto.rsq(i,i) <<  " " << ok.rsq(i,i) << endl;
	int lo = 1; int hi = N;
	while(lo + 1 < hi){
		int mid = (lo+hi)/2;
		if(ok.rsq(mid) <= a)
			lo = mid;
		else hi = mid-1;
	}
	//~ cout << lo << " " << hi << endl;
	ll a1, a2;
	if(ok.rsq((hi)) < a) a1 = BIG; else a1 = quanto.rsq(hi);
	if(ok.rsq((lo)) < a) a2 = BIG; else a2 = quanto.rsq(lo);
	return min(a1,a2);
}

typedef pair<ll,int> ii;
typedef pair<int,ii> iii;
ll ans;
priority_queue< iii, vector<iii>, greater<iii> > singole;
iii g(int a,ll b, int c){
	iii temp; temp.first = a; temp.second.first = b; temp.second.second = c; return temp;
}

bool compare(int x, int y){
	//~ if (x < 0 || x >= MAXN || y < 0 || y >= MAXN) {
		//~ cout << "B[" << x << "] = " << B[x] << ", ";
		//~ cout << "B[" << y << "] = " << B[y] << endl;
	//~ }
	return B[x] < B[y];
}

int main()
{
	//~ freopen("input.txt","r",stdin);

	cin >> N >> W;
	for(int i = 1; i <= N; ++i)
		state[i] = 0;
	for(int i = 1; i <= N; ++i)
		cin >> A[i]  >> B[i];
	for(int i = 1; i <= N; ++i){
		singole.push( g(A[i],B[i],i));
	}
	for(int i = 1; i <= N; ++i)
		O[i] = i;
	sort(O+1,O+N+1,compare);
	//~ cout << "HERE" << endl;
	for(int i = 1; i <= N; ++i){
		ok.adjust(i,1);
		quanto.adjust(i,B[O[i]]);
	}
	for(int i = 1; i<=N; ++i)
		C[O[i]] = i;
	ans = 0;
	ll best = 3e+14+10;
	int bestpos = 0;
	for(int i = 1; i <= W; ++i){
		iii front = singole.top(); singole.pop();
		ans+= front.first;;
		if(state[front.second.second] == 0) remove(front.second.second); // Rimuove dal bit che esiste coppia in posizione i
		if(state[front.second.second] == 0) singole.push( g(front.second.first-front.first,BIG,front.second.second));
		ll val = compute( (W-i) / 2 + ((W - i)%2));
		state[front.second.second]++;
		if(best > ans + val){
			bestpos = i;
			best = ans + val;
		}
	}
	//~ cout << best << endl;

	// OK ORA DEVO RIFARE
	ok.clear();
	quanto.clear();
	ans = 0;
	while(!singole.empty())singole.pop();

	for(int i = 1; i <= N; ++i){
		singole.push( g(A[i],B[i],i));
	}
	for(int i = 1; i <= N; ++i){
		state[i] = 0;
		ok.adjust(i,1);
		quanto.adjust(i,B[O[i]]);
	}
	for(int i = 1; i <= W; ++i){
		iii front = singole.top(); singole.pop();
		ans+= front.first;
		if(state[front.second.second] == 0) remove(front.second.second); // Rimuove dal bit che esiste coppia in posizione i
		if(state[front.second.second] == 0) singole.push( g(front.second.first-front.first,BIG,front.second.second));
		ll val = compute( (W-i) / 2 + ((W - i)%2));
		state[front.second.second]++;
		if(bestpos == i){
			int pos = 1;
			int q =  (W-i) / 2 + ((W - i)%2);
			cout << ans + val << endl;
			while(q){
				if(ok.rsq(pos,pos) == 1){
					state[O[pos]] = 2; --q;}
				++pos;
			}
			for(int i = 1; i <= N; ++i)
				cout << state[i];
			cout << endl;
			return 0;
		}
	}

}


#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define LSOne(S)(S & (-S))
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
vi copia;
int N,W;
const ll BIG = 99999999999999;
int state[300005];
int stateok[300005];
int O[300005];
int A[300005];
int B[300005];
int C[300005];
class FenwickTree{
	public:vi ft;
	FenwickTree(){ ft.assign(300005+1,0);}

	ll rsq(int b){
		ll sum = 0; for(;b;b-=LSOne(b)) sum+= ft[b]; return sum;}

	ll rsq(int a, int b){
		return rsq(b) - (a == 1 ? 0 : rsq(a-1));}

	void adjust(int k, int v){
		for(; k < (int)ft.size(); k+= LSOne(k)) ft[k] += v;}

	void clear() {
		ft.clear();
		ft.assign(300005+1,0);
	}
};

FenwickTree ok;
FenwickTree quanto;

void remove(int pos){
	ok.adjust(C[pos], -1 );
	quanto.adjust(C[pos], -B[O[C[pos]]] );
}

ll compute(int a){
	if(a==0) return 0;
	//~ cout << "Computing " << a << endl;
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << "i :" <<  quanto.rsq(i,i) <<  " " << ok.rsq(i,i) << endl;
	int lo = 1; int hi = N;
	while(lo + 1 < hi){
		int mid = (lo+hi)/2;
		if(ok.rsq(mid) <= a)
			lo = mid;
		else hi = mid-1;
	}
	//~ cout << lo << " " << hi << endl;
	ll a1, a2;
	if(ok.rsq((hi)) < a) a1 = BIG; else a1 = quanto.rsq(hi);
	if(ok.rsq((lo)) < a) a2 = BIG; else a2 = quanto.rsq(lo);
	return min(a1,a2);
}

typedef pair<ll,int> ii;
typedef pair<int,ii> iii;
ll ans;
priority_queue< iii, vector<iii>, greater<iii> > singole;
iii g(int a,ll b, int c){
	iii temp; temp.first = a; temp.second.first = b; temp.second.second = c; return temp;
}

bool compare(int x, int y){
	//~ cout << "B[" << a << "] = " << B[a] << ", ";
	//~ cout << "B[" << b << "] = " << B[b] << endl;
	return B[x] <= B[y];
}

int main()
{
	//~ freopen("input.txt","r",stdin);

	cin >> N >> W;
	for(int i = 1; i <= N; ++i)
		state[i] = 0;
	for(int i = 1; i <= N; ++i)
		cin >> A[i]  >> B[i];
	for(int i = 1; i <= N; ++i){
		singole.push( g(A[i],B[i],i));
	}
	for(int i = 1; i <= N; ++i)
		O[i] = i;
	sort(O+1,O+N+1,compare);
	for(int i = 1; i <= N; ++i){
		ok.adjust(i,1);
		quanto.adjust(i,B[O[i]]);
	}
	for(int i = 1; i<=N; ++i)
		C[O[i]] = i;
	ans = 0;
	ll best = 99999999999999;
	int bestpos = 0;
	for(int i = 1; i <= W; ++i){
		iii front = singole.top(); singole.pop();
		ans+= front.first;;
		if(state[front.second.second] == 0) remove(front.second.second); // Rimuove dal bit che esiste coppia in posizione i
		if(state[front.second.second] == 0) singole.push( g(front.second.first-front.first,BIG,front.second.second));
		ll val = compute( (W-i) / 2 + ((W - i)%2));
		state[front.second.second]++;
		if(best > ans + val){
			bestpos = i;
			best = ans + val;
		}
	}
	//~ cout << best << endl;

	// OK ORA DEVO RIFARE
	ok.clear();
	quanto.clear();
	ans = 0;
	while(!singole.empty())singole.pop();

	for(int i = 1; i <= N; ++i){
		singole.push( g(A[i],B[i],i));
	}
	for(int i = 1; i <= N; ++i){
		state[i] = 0;
		ok.adjust(i,1);
		quanto.adjust(i,B[O[i]]);
	}
	for(int i = 1; i <= W; ++i){
		iii front = singole.top(); singole.pop();
		ans+= front.first;
		if(state[front.second.second] == 0) remove(front.second.second); // Rimuove dal bit che esiste coppia in posizione i
		if(state[front.second.second] == 0) singole.push( g(front.second.first-front.first,BIG,front.second.second));
		ll val = compute( (W-i) / 2 + ((W - i)%2));
		state[front.second.second]++;
		if(bestpos == i){
			int pos = 1;
			int q =  (W-i) / 2 + ((W - i)%2);
			cout << ans + val << endl;
			while(q){
				if(ok.rsq(pos,pos) == 1){
					state[O[pos]] = 2; --q;}
				++pos;
			}
			for(int i = 1; i <= N; ++i)
				cout << state[i];
			cout << endl;
			return 0;
		}
	}

}


#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int N,W;
typedef pair<int,int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
iii g(int a, int b , int c){
	iii temp; temp.first = a; temp.second.first = b; temp.second.second = c; return temp;}

priority_queue< iii, vector<iii>, greater<iii> > singole;
vii doppie;
int state[300005];
int A[300005];
int B[300005];

int main()
{
	 //~ freopen("input.txt","r",stdin);
	cin >> N >> W;
	for(int i = 0; i < N; ++i)
		state[i] = 1;
	for(int i = 0; i < N; ++i)
		cin >> A[i]  >> B[i];

	for(int i = 0; i < N; ++i){
		singole.push( g(A[i], 1, i));
		doppie.push_back( ii(B[i],i));
	}
	sort(doppie.begin(),doppie.end());
	int cur = 0;
	int ans = 0;
	int stelle = 0;
	while(true){
		// SE MI MANCA SOLO UNA STELLA LA METTO
	    if(W - stelle == 1){
			iii s;
			do{
				s = singole.top(); singole.pop();
			}while(singole.size() != 0 && state[s.second.second] != s.second.first);
			stelle++; ans+= s.first;
			state[s.second.second]++;
		}

		if(stelle >= W) break;

		// CALCOLO DOVE SONO CON LE DOPPIE
		while(cur < N && state[doppie[cur].second] != 1) ++cur;

		// SE HO FINITO LE DOPPIE METTO SOLO LE SINGOLE
		if(cur >= N){
			while(ans < W){
				iii f;
				do{
					f = singole.top(); singole.pop();
				}while(singole.size() != 0 && state[f.second.second] != f.second.first);
				state[f.second.second] = 3;
				stelle++;
				ans+= f.first;
			}
		}

		// IN CASO CONTRARIO POSSO VEDERE SE CI SONO LE SINGOLE
		else{

			iii f;
			do{
				f = singole.top(); singole.pop();
			}while(singole.size() != 0 && state[f.second.second] != f.second.first);

			iii s;
			do{

				// SE C'E' SOLO UNA SINGOLA VUOL DIRE CHE C'E' ANCHE SOLO UNA DOPPIA QUINDI METTO QUELLO CHE MI SERVE
				if(singole.size() == 0){
					if(W-stelle == 1){ stelle++; ans+= f.first; state[f.second.second]++;}
					else if(W-stelle == 2){stelle+=2; ans+= doppie[cur].first; cur++; state[doppie[cur-1].second] = 3;}
				}
				else s = singole.top(); singole.pop();
			}while(singole.size() != 0 && state[s.second.second] != s.second.first);

			if(ans == W) break;
			// ORA VALUTO CHE FARE
			if( doppie[cur].first  >= f.first + s.first){
				//~ cout << doppie[cur].first << "  >=  " << f.first <<  " + " <<  s.first << endl;
				ans+= f.first;
				ans+= s.first;
				stelle+=2;
				if(state[f.second.second] == 2){ state[f.second.second] = 3;}
				if(state[s.second.second] == 2){ state[s.second.second] = 3;}
				if(state[f.second.second] == 1){ singole.push( g( B[f.second.second] - A[f.second.second], 2, f.second.second));state[f.second.second] = 2;}
				if(state[s.second.second] == 1){ singole.push( g( B[s.second.second] - A[s.second.second], 2, s.second.second));state[s.second.second] = 2;}
			}else{
				ans+= doppie[cur].first;
				cur++;
				state[doppie[cur-1].second] = 3;
				singole.push(f);
				singole.push(s);
				stelle+=2;
			}
		}
	}
	cout << ans << endl;
	for(int i = 0; i < N; ++i)
		cout << state[i]-1;
	cout << endl;
}


#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define LSOne(S)(S & (-S))
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
vi copia;
int N,W;
const ll BIG = 999999999999;
int state[300005];
int stateok[300005];
int O[300005];
int A[300005];
int B[300005];
int C[300005];
class FenwickTree{
	public:vi ft;
	FenwickTree(){ ft.assign(300005+1,0);}

	ll rsq(int b){
		ll sum = 0; for(;b;b-=LSOne(b)) sum+= ft[b]; return sum;}

	ll rsq(int a, int b){
		return rsq(b) - (a == 1 ? 0 : rsq(a-1));}

	void adjust(int k, int v){
		for(; k < (int)ft.size(); k+= LSOne(k)) ft[k] += v;}

	void clear() {
		ft.clear();
		ft.assign(300005+1,0);
	}
};

FenwickTree ok;
FenwickTree quanto;

void remove(int pos){
	ok.adjust(C[pos], -1 );
	quanto.adjust(C[pos], -B[O[C[pos]]] );
}

ll compute(int a){
	if(a==0) return 0;
	//~ cout << "Computing " << a << endl;
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << "i :" <<  quanto.rsq(i,i) <<  " " << ok.rsq(i,i) << endl;
	int lo = 1; int hi = N;
	while(lo + 1 < hi){
		int mid = (lo+hi)/2;
		if(ok.rsq(mid) <= a)
			lo = mid;
		else hi = mid-1;
	}
	//~ cout << lo << " " << hi << endl;
	ll a1, a2;
	if(ok.rsq((hi)) < a) a1 = BIG; else a1 = quanto.rsq(hi);
	if(ok.rsq((lo)) < a) a2 = BIG; else a2 = quanto.rsq(lo);
	return min(a1,a2);
}

typedef pair<ll,int> ii;
typedef pair<int,ii> iii;
ll ans;
priority_queue< iii, vector<iii>, greater<iii> > singole;
iii g(int a,ll b, int c){
	iii temp; temp.first = a; temp.second.first = b; temp.second.second = c; return temp;
}

bool compare(int x, int y){
	//~ cout << "B[" << a << "] = " << B[a] << ", ";
	//~ cout << "B[" << b << "] = " << B[b] << endl;
	return B[x] <= B[y];
}

int main()
{
	//~ freopen("input.txt","r",stdin);

	cin >> N >> W;
	for(int i = 1; i <= N; ++i)
		state[i] = 0;
	for(int i = 1; i <= N; ++i)
		cin >> A[i]  >> B[i];
	for(int i = 1; i <= N; ++i){
		singole.push( g(A[i],B[i],i));
	}
	for(int i = 1; i <= N; ++i)
		O[i] = i;
	sort(O+1,O+N+1,compare);
	for(int i = 1; i <= N; ++i){
		ok.adjust(i,1);
		quanto.adjust(i,B[O[i]]);
	}
	for(int i = 1; i<=N; ++i)
		C[O[i]] = i;
	ans = 0;
	ll best = 999999999999;
	int bestpos = 0;
	for(int i = 1; i <= W; ++i){
		iii front = singole.top(); singole.pop();
		ans+= front.first;;
		if(state[front.second.second] == 0) remove(front.second.second); // Rimuove dal bit che esiste coppia in posizione i
		if(state[front.second.second] == 0) singole.push( g(front.second.first-front.first,BIG,front.second.second));
		ll val = compute( (W-i) / 2 + ((W - i)%2));
		state[front.second.second]++;
		if(best > ans + val){
			bestpos = i;
			best = ans + val;
		}
	}

	// OK ORA DEVO RIFARE
	ok.clear();
	quanto.clear();
	while(!singole.empty())singole.pop();

	for(int i = 1; i <= N; ++i){
		singole.push( g(A[i],B[i],i));
	}
	for(int i = 1; i <= N; ++i){
		state[i] = 0;
		ok.adjust(i,1);
		quanto.adjust(i,B[O[i]]);
	}
	for(int i = 1; i<=N; ++i)
		C[O[i]] = i;
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << C[i] << "  ";
	//~ cout << endl;

	//~ ans = 0;
	//~ ll best = 999999999999;
	//~ int bestpos = 0;
	for(int i = 1; i <= W; ++i){
		iii front = singole.top(); singole.pop();
		ans+= front.first;
		//~ cout << "Considering " << i << " single stars " << " with time " << ans << endl;
		if(state[front.second.second] == 0) remove(front.second.second); // Rimuove dal bit che esiste coppia in posizione i
		if(state[front.second.second] == 0) singole.push( g(front.second.first-front.first,BIG,front.second.second));
		//~ cout << "Pushing " << front.second.first - front.first <<  " , " << BIG << " , " << i << endl;
		ll val = compute( (W-i) / 2 + ((W - i)%2));
		//~ cout << "Have to add " << val << " time" << endl << endl;
		state[front.second.second]++;
		if(bestpos == i){
			int pos = 1;
			int q =  (W-i) / 2 + ((W - i)%2);
			cout << ans + val << endl;
			while(q){
				if(ok.rsq(pos,pos) == 1){
					state[C[pos]] = 2; --q;}
				++pos;
			}
			for(int i = 1; i <= N; ++i)
				cout << state[i];
			cout << endl;
		}
	}

}


#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define LSOne(S)(S & (-S))
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
vi copia;
int N,W;
const ll BIG = 999999999999;
int state[300005];
int stateok[300005];
int O[300005];
int A[300005];
int B[300005];
int C[300005];
class FenwickTree{
	public:vi ft;
	FenwickTree(){ ft.assign(300005+1,0);}

	ll rsq(int b){
		ll sum = 0; for(;b;b-=LSOne(b)) sum+= ft[b]; return sum;}

	ll rsq(int a, int b){
		return rsq(b) - (a == 1 ? 0 : rsq(a-1));}

	void adjust(int k, int v){
		for(; k < (int)ft.size(); k+= LSOne(k)) ft[k] += v;}

	void clear() {
		ft.clear();
		ft.assign(300005+1,0);
	}
};

FenwickTree ok;
FenwickTree quanto;

void remove(int pos){
	ok.adjust(C[pos], -1 );
	quanto.adjust(C[pos], -B[O[C[pos]]] );
}

ll compute(int a){
	if(a==0) return 0;
	//~ cout << "Computing " << a << endl;
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << "i :" <<  quanto.rsq(i,i) <<  " " << ok.rsq(i,i) << endl;
	int lo = 1; int hi = N;
	while(lo + 1 < hi){
		int mid = (lo+hi)/2;
		if(ok.rsq(mid) <= a)
			lo = mid;
		else hi = mid-1;
	}
	//~ cout << lo << " " << hi << endl;
	ll a1, a2;
	if(ok.rsq((hi)) < a) a1 = BIG; else a1 = quanto.rsq(hi);
	if(ok.rsq((lo)) < a) a2 = BIG; else a2 = quanto.rsq(lo);
	return min(a1,a2);
}

typedef pair<ll,int> ii;
typedef pair<int,ii> iii;
ll ans;
priority_queue< iii, vector<iii>, greater<iii> > singole;
iii g(int a,ll b, int c){
	iii temp; temp.first = a; temp.second.first = b; temp.second.second = c; return temp;
}

bool compare(int x, int y){
	//~ cout << "B[" << a << "] = " << B[a] << ", ";
	//~ cout << "B[" << b << "] = " << B[b] << endl;
	return B[x] <= B[y];
}

int main()
{
	freopen("input.txt","r",stdin);

	cin >> N >> W;
	for(int i = 1; i <= N; ++i)
		state[i] = 0;
	for(int i = 1; i <= N; ++i)
		cin >> A[i]  >> B[i];
	for(int i = 1; i <= N; ++i){
		singole.push( g(A[i],B[i],i));
	}
	for(int i = 1; i <= N; ++i)
		O[i] = i;
	sort(O+1,O+N+1,compare);
	for(int i = 1; i <= N; ++i){
		ok.adjust(i,1);
		quanto.adjust(i,B[O[i]]);
	}
	for(int i = 1; i<=N; ++i)
		C[O[i]] = i;
	ans = 0;
	ll best = 999999999999;
	int bestpos = 0;
	for(int i = 1; i <= W; ++i){
		iii front = singole.top(); singole.pop();
		ans+= front.first;;
		if(state[front.second.second] == 0) remove(front.second.second); // Rimuove dal bit che esiste coppia in posizione i
		if(state[front.second.second] == 0) singole.push( g(front.second.first-front.first,BIG,front.second.second));
		ll val = compute( (W-i) / 2 + ((W - i)%2));
		state[front.second.second]++;
		if(best > ans + val){
			bestpos = i;
			best = ans + val;
		}
	}
	//~ cout << best << endl;

	// OK ORA DEVO RIFARE
	ok.clear();
	quanto.clear();
	ans = 0;
	while(!singole.empty())singole.pop();

	for(int i = 1; i <= N; ++i){
		singole.push( g(A[i],B[i],i));
	}
	for(int i = 1; i <= N; ++i){
		state[i] = 0;
		ok.adjust(i,1);
		quanto.adjust(i,B[O[i]]);
	}
	for(int i = 1; i <= W; ++i){
		iii front = singole.top(); singole.pop();
		ans+= front.first;
		if(state[front.second.second] == 0) remove(front.second.second); // Rimuove dal bit che esiste coppia in posizione i
		if(state[front.second.second] == 0) singole.push( g(front.second.first-front.first,BIG,front.second.second));
		ll val = compute( (W-i) / 2 + ((W - i)%2));
		state[front.second.second]++;
		if(bestpos == i){
			int pos = 1;
			int q =  (W-i) / 2 + ((W - i)%2);
			cout << ans + val << endl;
			while(q){
				if(ok.rsq(pos,pos) == 1){
					state[C[pos]] = 2; --q;}
				++pos;
			}
			for(int i = 1; i <= N; ++i)
				cout << state[i];
			cout << endl;
			return 0;
		}
	}

}


#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define LSOne(S)(S & (-S))
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
vi copia;
int N,W;
const ll BIG = 99999999;
int state[300005];
int stateok[300005];
int O[300005];
int A[300005];
int B[300005];
int C[300005];
class FenwickTree{
	public:vi ft;
	FenwickTree(){ ft.assign(300005+1,0);}

	ll rsq(int b){
		int sum = 0; for(;b;b-=LSOne(b)) sum+= ft[b]; return sum;}

	ll rsq(int a, int b){
		return rsq(b) - (a == 1 ? 0 : rsq(a-1));}

	void adjust(int k, int v){
		for(; k < (int)ft.size(); k+= LSOne(k)) ft[k] += v;}
};

FenwickTree ok;
FenwickTree quanto;

void remove(int pos){
	ok.adjust(C[pos], -1 );
	quanto.adjust(C[pos], -B[O[C[pos]]] );
}

ll compute(int a){
	if(a == 0) return 0;
	//~ cout << "Computing " << a << endl;
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << "i :" <<  quanto.rsq(i,i) <<  " " << ok.rsq(i,i) << endl;
	int lo = 1; int hi = N;
	while(lo + 1 < hi){
		int mid = (lo+hi)/2;
		if(ok.rsq(mid) <= a)
			lo = mid;
		else hi = mid-1;
	}
	//~ cout << lo << " " << hi << endl;
	ll a1, a2;
	if(ok.rsq((hi)) < a) a1 = BIG; else a1 = quanto.rsq(hi);
	if(ok.rsq((lo)) < a) a2 = BIG; else a2 = quanto.rsq(lo);

	return min(a1,a2);
}

typedef pair<int,int> ii;
typedef pair<int,ii> iii;
ll ans;
priority_queue< iii, vector<iii>, greater<iii> > singole;
iii g(int a,int b, int c){
	iii temp; temp.first = a; temp.second.first = b; temp.second.second = c; return temp;
}

bool compare(int x, int y){
	//~ cout << "B[" << a << "] = " << B[a] << ", ";
	//~ cout << "B[" << b << "] = " << B[b] << endl;
	return B[x] <= B[y];
}

int main()
{
	//~ freopen("input.txt","r",stdin);

	cin >> N >> W;
	for(int i = 1; i <= N; ++i)
		state[i] = 0;
	for(int i = 1; i <= N; ++i)
		cin >> A[i]  >> B[i];

	for(int i = 1; i <= N; ++i){
		singole.push( g(A[i],B[i],i));
	}

	for(int i = 1; i <= N; ++i)
		O[i] = i;

	//~ for(int i = 1; i <= N; ++i)
		//~ cout << A[i] << "    ";
	//~ cout << endl;
	//~
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << B[i] << "    ";
	//~ cout << endl;

	sort(O+1,O+N+1,compare);

	for(int i = 1; i <= N; ++i){
		ok.adjust(i,1);
		quanto.adjust(i,B[O[i]]);
	}
	for(int i = 1; i<=N; ++i)
		C[O[i]] = i;
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << C[i] << "  ";
	//~ cout << endl;

	ans = 0;
	ll best = 9999999;
	int bestpos = 0;
	for(int i = 1; i <= W; ++i){
		iii front = singole.top(); singole.pop();
		ans+= front.first;
		//~ cout << "Considering " << i << " single stars " << " with time " << ans << endl;
		if(state[front.second.second] == 0) remove(front.second.second); // Rimuove dal bit che esiste coppia in posizione i
		if(state[front.second.second] == 0) singole.push( g(front.second.first-front.first,BIG,front.second.second));
		//~ cout << "Pushing " << front.second.first - front.first <<  " , " << BIG << " , " << i << endl;
		int cosa = (W-i)/2; if((W-i) >= 2) cosa += (W-i)% 2;
		ll val =  compute( cosa);
		//~ cout << "Have to add " << val << " time" << endl << endl;
		state[front.second.second]++;
		if(best > ans + val){
			copia = ok.ft;
			bestpos = i;
			best = ans + val;
			memcpy(stateok,state,sizeof(state));
		}
	}
	cout << best << endl;
	int q =  (W-bestpos)/2;if(W-bestpos >= 2) q += (W-bestpos)% 2;
	//cout << q << endl;
	int pos = 1;
	ok.ft = copia;

	while(q){
		if(ok.rsq(pos,pos) == 1){
			stateok[C[pos]] = 2; --q;}
		++pos;
	}
	for(int i = 1; i <= N; ++i)
		cout << stateok[i];
	cout << endl;
}


#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define LSOne(S)(S & (-S))
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
vi copia;
int N,W;
const ll BIG = 999999999999;
int state[300005];
int stateok[300005];
int O[300005];
int A[300005];
int B[300005];
int C[300005];
class FenwickTree{
	public:vi ft;
	FenwickTree(){ ft.assign(300005+1,0);}

	ll rsq(int b){
		ll sum = 0; for(;b;b-=LSOne(b)) sum+= ft[b]; return sum;}

	ll rsq(int a, int b){
		return rsq(b) - (a == 1 ? 0 : rsq(a-1));}

	void adjust(int k, int v){
		for(; k < (int)ft.size(); k+= LSOne(k)) ft[k] += v;}

	void clear() {
		ft.clear();
		ft.assign(300005+1,0);
	}
};

FenwickTree ok;
FenwickTree quanto;

void remove(int pos){
	ok.adjust(C[pos], -1 );
	quanto.adjust(C[pos], -B[O[C[pos]]] );
}

ll compute(int a){
	if(a==0) return 0;
	//~ cout << "Computing " << a << endl;
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << "i :" <<  quanto.rsq(i,i) <<  " " << ok.rsq(i,i) << endl;
	int lo = 1; int hi = N;
	while(lo + 1 < hi){
		int mid = (lo+hi)/2;
		if(ok.rsq(mid) <= a)
			lo = mid;
		else hi = mid-1;
	}
	//~ cout << lo << " " << hi << endl;
	ll a1, a2;
	if(ok.rsq((hi)) < a) a1 = BIG; else a1 = quanto.rsq(hi);
	if(ok.rsq((lo)) < a) a2 = BIG; else a2 = quanto.rsq(lo);
	return min(a1,a2);
}

typedef pair<ll,int> ii;
typedef pair<int,ii> iii;
ll ans;
priority_queue< iii, vector<iii>, greater<iii> > singole;
iii g(int a,ll b, int c){
	iii temp; temp.first = a; temp.second.first = b; temp.second.second = c; return temp;
}

bool compare(int x, int y){
	//~ cout << "B[" << a << "] = " << B[a] << ", ";
	//~ cout << "B[" << b << "] = " << B[b] << endl;
	return B[x] <= B[y];
}

int main()
{
	//~ freopen("input.txt","r",stdin);

	cin >> N >> W;
	for(int i = 1; i <= N; ++i)
		state[i] = 0;
	for(int i = 1; i <= N; ++i)
		cin >> A[i]  >> B[i];
	for(int i = 1; i <= N; ++i){
		singole.push( g(A[i],B[i],i));
	}
	for(int i = 1; i <= N; ++i)
		O[i] = i;
	sort(O+1,O+N+1,compare);
	for(int i = 1; i <= N; ++i){
		ok.adjust(i,1);
		quanto.adjust(i,B[O[i]]);
	}
	for(int i = 1; i<=N; ++i)
		C[O[i]] = i;
	ans = 0;
	ll best = 9999999999999;
	int bestpos = 0;
	for(int i = 1; i <= W; ++i){
		iii front = singole.top(); singole.pop();
		ans+= front.first;;
		if(state[front.second.second] == 0) remove(front.second.second); // Rimuove dal bit che esiste coppia in posizione i
		if(state[front.second.second] == 0) singole.push( g(front.second.first-front.first,BIG,front.second.second));
		ll val = compute( (W-i) / 2 + ((W - i)%2));
		state[front.second.second]++;
		if(best > ans + val){
			bestpos = i;
			best = ans + val;
		}
	}
	// cout << best << endl;

	// OK ORA DEVO RIFARE
	ok.clear();
	quanto.clear();
	ans = 0;
	while(!singole.empty())singole.pop();

	for(int i = 1; i <= N; ++i){
		singole.push( g(A[i],B[i],i));
	}
	for(int i = 1; i <= N; ++i){
		state[i] = 0;
		ok.adjust(i,1);
		quanto.adjust(i,B[O[i]]);
	}
	for(int i = 1; i <= W; ++i){
		iii front = singole.top(); singole.pop();
		ans+= front.first;
		if(state[front.second.second] == 0) remove(front.second.second); // Rimuove dal bit che esiste coppia in posizione i
		if(state[front.second.second] == 0) singole.push( g(front.second.first-front.first,BIG,front.second.second));
		ll val = compute( (W-i) / 2 + ((W - i)%2));
		state[front.second.second]++;
		if(bestpos == i){
			int pos = 1;
			int q =  (W-i) / 2 + ((W - i)%2);
			cout << ans + val << endl;
			while(q){
				if(ok.rsq(pos,pos) == 1){
					state[C[pos]] = 2; --q;}
				++pos;
			}
			for(int i = 1; i <= N; ++i)
				cout << state[i];
			cout << endl;
			return 0;
		}
	}

}


#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define LSOne(S)(S & (-S))
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
vi copia;
int N,W;
const ll BIG = 99999999;
int state[300005];
int stateok[300005];
int O[300005];
int A[300005];
int B[300005];
int C[300005];
class FenwickTree{
	public:vi ft;
	FenwickTree(){ ft.assign(300005+1,0);}

	ll rsq(int b){
		int sum = 0; for(;b;b-=LSOne(b)) sum+= ft[b]; return sum;}

	ll rsq(int a, int b){
		return rsq(b) - (a == 1 ? 0 : rsq(a-1));}

	void adjust(int k, int v){
		for(; k < (int)ft.size(); k+= LSOne(k)) ft[k] += v;}
};

FenwickTree ok;
FenwickTree quanto;

void remove(int pos){
	ok.adjust(C[pos], -1 );
	quanto.adjust(C[pos], -B[O[C[pos]]] );
}

ll compute(int a){
	//~ cout << "Computing " << a << endl;
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << "i :" <<  quanto.rsq(i,i) <<  " " << ok.rsq(i,i) << endl;
	int lo = 1; int hi = N;
	while(lo + 1 < hi){
		int mid = (lo+hi)/2;
		if(ok.rsq(mid) <= a)
			lo = mid;
		else hi = mid-1;
	}
	//~ cout << lo << " " << hi << endl;
	ll a1, a2;
	if(ok.rsq((hi)) < a) a1 = BIG; else a1 = quanto.rsq(hi);
	if(ok.rsq((lo)) < a) a2 = BIG; else a2 = quanto.rsq(lo);

	return min(a1,a2);
}

typedef pair<int,int> ii;
typedef pair<int,ii> iii;
ll ans;
priority_queue< iii, vector<iii>, greater<iii> > singole;
iii g(int a,int b, int c){
	iii temp; temp.first = a; temp.second.first = b; temp.second.second = c; return temp;
}

bool compare(int x, int y){
	//~ cout << "B[" << a << "] = " << B[a] << ", ";
	//~ cout << "B[" << b << "] = " << B[b] << endl;
	return B[x] <= B[y];
}

int main()
{
	freopen("input.txt","r",stdin);

	cin >> N >> W;
	for(int i = 1; i <= N; ++i)
		state[i] = 0;
	for(int i = 1; i <= N; ++i)
		cin >> A[i]  >> B[i];

	for(int i = 1; i <= N; ++i){
		singole.push( g(A[i],B[i],i));
	}

	for(int i = 1; i <= N; ++i)
		O[i] = i;

	//~ for(int i = 1; i <= N; ++i)
		//~ cout << A[i] << "    ";
	//~ cout << endl;
	//~
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << B[i] << "    ";
	//~ cout << endl;

	sort(O+1,O+N+1,compare);

	for(int i = 1; i <= N; ++i){
		ok.adjust(i,1);
		quanto.adjust(i,B[O[i]]);
	}
	for(int i = 1; i<=N; ++i)
		C[O[i]] = i;
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << C[i] << "  ";
	//~ cout << endl;

	ans = 0;
	ll best = 9999999;
	int bestpos = 0;
	for(int i = 1; i <= W; ++i){
		iii front = singole.top(); singole.pop();
		ans+= front.first;
		//~ cout << "Considering " << i << " single stars " << " with time " << ans << endl;
		if(state[front.second.second] == 0) remove(front.second.second); // Rimuove dal bit che esiste coppia in posizione i
		if(state[front.second.second] == 0) singole.push( g(front.second.first-front.first,BIG,front.second.second));
		//~ cout << "Pushing " << front.second.first - front.first <<  " , " << BIG << " , " << i << endl;
		ll val = compute( (W-i) / 2 + ((W - i)%2));
		//~ cout << "Have to add " << val << " time" << endl << endl;
		state[front.second.second]++;
		if(best > ans + val){
			copia = ok.ft;
			bestpos = i;
			best = ans + val;
			memcpy(stateok,state,sizeof(state));
		}
	}
	cout << best << endl;
	int q = ( W - bestpos)/2 + ((W - bestpos)%2);
	//cout << q << endl;
	int pos = 1;
	ok.ft = copia;

	while(q){
		if(ok.rsq(pos,pos) == 1){
			stateok[C[pos]] = 2; --q;}
		++pos;
	}
	for(int i = 1; i <= N; ++i)
		cout << stateok[i];
	cout << endl;
}


#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define LSOne(S)(S & (-S))
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
vi copia;
int N,W;
const ll BIG = 999999999999;
int state[300005];
int stateok[300005];
int O[300005];
int A[300005];
int B[300005];
int C[300005];
class FenwickTree{
	public:vi ft;
	FenwickTree(){ ft.assign(300005+1,0);}

	ll rsq(int b){
		ll sum = 0; for(;b;b-=LSOne(b)) sum+= ft[b]; return sum;}

	ll rsq(int a, int b){
		return rsq(b) - (a == 1 ? 0 : rsq(a-1));}

	void adjust(int k, int v){
		for(; k < (int)ft.size(); k+= LSOne(k)) ft[k] += v;}

	void clear() {
		ft.clear();
		ft.assign(300005+1,0);
	}
};

FenwickTree ok;
FenwickTree quanto;

void remove(int pos){
	ok.adjust(C[pos], -1 );
	quanto.adjust(C[pos], -B[O[C[pos]]] );
}

ll compute(int a){
	if(a==0) return 0;
	//~ cout << "Computing " << a << endl;
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << "i :" <<  quanto.rsq(i,i) <<  " " << ok.rsq(i,i) << endl;
	int lo = 1; int hi = N;
	while(lo + 1 < hi){
		int mid = (lo+hi)/2;
		if(ok.rsq(mid) <= a)
			lo = mid;
		else hi = mid-1;
	}
	//~ cout << lo << " " << hi << endl;
	ll a1, a2;
	if(ok.rsq((hi)) < a) a1 = BIG; else a1 = quanto.rsq(hi);
	if(ok.rsq((lo)) < a) a2 = BIG; else a2 = quanto.rsq(lo);
	return min(a1,a2);
}

typedef pair<ll,int> ii;
typedef pair<int,ii> iii;
ll ans;
priority_queue< iii, vector<iii>, greater<iii> > singole;
iii g(int a,ll b, int c){
	iii temp; temp.first = a; temp.second.first = b; temp.second.second = c; return temp;
}

bool compare(int x, int y){
	//~ cout << "B[" << a << "] = " << B[a] << ", ";
	//~ cout << "B[" << b << "] = " << B[b] << endl;
	return B[x] <= B[y];
}

int main()
{
	//~ freopen("input.txt","r",stdin);

	cin >> N >> W;
	for(int i = 1; i <= N; ++i)
		state[i] = 0;
	for(int i = 1; i <= N; ++i)
		cin >> A[i]  >> B[i];
	for(int i = 1; i <= N; ++i){
		singole.push( g(A[i],B[i],i));
	}
	for(int i = 1; i <= N; ++i)
		O[i] = i;
	sort(O+1,O+N+1,compare);
	for(int i = 1; i <= N; ++i){
		ok.adjust(i,1);
		quanto.adjust(i,B[O[i]]);
	}
	for(int i = 1; i<=N; ++i)
		C[O[i]] = i;
	ans = 0;
	ll best = 999999999999;
	int bestpos = 0;
	for(int i = 1; i <= W; ++i){
		iii front = singole.top(); singole.pop();
		ans+= front.first;;
		if(state[front.second.second] == 0) remove(front.second.second); // Rimuove dal bit che esiste coppia in posizione i
		if(state[front.second.second] == 0) singole.push( g(front.second.first-front.first,BIG,front.second.second));
		ll val = compute( (W-i) / 2 + ((W - i)%2));
		state[front.second.second]++;
		if(best > ans + val){
			bestpos = i;
			best = ans + val;
		}
	}

	// OK ORA DEVO RIFARE

	while(!singole.empty())singole.pop();

	for(int i = 1; i <= N; ++i){
		singole.push( g(A[i],B[i],i));
	}
	for(int i = 1; i <= N; ++i){
		state[i] = 0;
		ok.adjust(i,1);
		quanto.adjust(i,B[O[i]]);
	}
	for(int i = 1; i<=N; ++i)
		C[O[i]] = i;
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << C[i] << "  ";
	//~ cout << endl;

	//~ ans = 0;
	//~ ll best = 999999999999;
	//~ int bestpos = 0;
	for(int i = 1; i <= W; ++i){
		iii front = singole.top(); singole.pop();
		ans+= front.first;
		//~ cout << "Considering " << i << " single stars " << " with time " << ans << endl;
		if(state[front.second.second] == 0) remove(front.second.second); // Rimuove dal bit che esiste coppia in posizione i
		if(state[front.second.second] == 0) singole.push( g(front.second.first-front.first,BIG,front.second.second));
		//~ cout << "Pushing " << front.second.first - front.first <<  " , " << BIG << " , " << i << endl;
		ll val = compute( (W-i) / 2 + ((W - i)%2));
		//~ cout << "Have to add " << val << " time" << endl << endl;
		state[front.second.second]++;
		if(bestpos == i){
			int pos = 1;
			int q =  (W-i) / 2 + ((W - i)%2);
			cout << ans + val << endl;
			while(q){
				if(ok.rsq(pos,pos) == 1){
					state[C[pos]] = 2; --q;}
				++pos;
			}
			for(int i = 1; i <= N; ++i)
				cout << state[i];
			cout << endl;
		}
	}

}


#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define LSOne(S)(S & (-S))
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
vi copia;
int N,W;
const ll BIG = 99999999;
int state[300005];
int O[300005];
int A[300005];
int B[300005];
int C[300005];
class FenwickTree{
	public:vi ft;
	FenwickTree(){ ft.assign(300005+1,0);}

	ll rsq(int b){
		int sum = 0; for(;b;b-=LSOne(b)) sum+= ft[b]; return sum;}

	ll rsq(int a, int b){
		return rsq(b) - (a == 1 ? 0 : rsq(a-1));}

	void adjust(int k, int v){
		for(; k < (int)ft.size(); k+= LSOne(k)) ft[k] += v;}
};

FenwickTree ok;
FenwickTree quanto;

void remove(int pos){
	ok.adjust(C[pos], -1 );
	quanto.adjust(C[pos], -B[O[C[pos]]] );
}

ll compute(int a){
	//~ cout << "Computing " << a << endl;
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << "i :" <<  quanto.rsq(i,i) <<  " " << ok.rsq(i,i) << endl;
	int lo = 1; int hi = N;
	while(lo + 1 < hi){
		int mid = (lo+hi)/2;
		if(ok.rsq(mid) <= a)
			lo = mid;
		else hi = mid-1;
	}
	//~ cout << lo << " " << hi << endl;
	ll a1, a2;
	if(ok.rsq((hi)) < a) a1 = BIG; else a1 = quanto.rsq(hi);
	if(ok.rsq((lo)) < a) a2 = BIG; else a2 = quanto.rsq(lo);

	return min(a1,a2);
}

typedef pair<int,int> ii;
typedef pair<int,ii> iii;
ll ans;
priority_queue< iii, vector<iii>, greater<iii> > singole;
iii g(int a,int b, int c){
	iii temp; temp.first = a; temp.second.first = b; temp.second.second = c; return temp;
}

bool compare(int x, int y){
	//~ cout << "B[" << a << "] = " << B[a] << ", ";
	//~ cout << "B[" << b << "] = " << B[b] << endl;
	return B[x] <= B[y];
}

int main()
{
	//~ freopen("input.txt","r",stdin);
	cin >> N >> W;
	for(int i = 1; i <= N; ++i)
		state[i] = 0;
	for(int i = 1; i <= N; ++i)
		cin >> A[i]  >> B[i];

	for(int i = 1; i <= N; ++i){
		singole.push( g(A[i],B[i],i));
	}

	for(int i = 1; i <= N; ++i)
		O[i] = i;

	//~ for(int i = 1; i <= N; ++i)
		//~ cout << A[i] << "    ";
	//~ cout << endl;
	//~
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << B[i] << "    ";
	//~ cout << endl;

	sort(O+1,O+N+1,compare);

	for(int i = 1; i <= N; ++i){
		ok.adjust(i,1);
		quanto.adjust(i,B[O[i]]);
	}
	for(int i = 1; i<=N; ++i)
		C[O[i]] = i;
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << C[i] << "  ";
	//~ cout << endl;

	ans = 0;
	ll best = 9999999;
	int bestpos = 0;
	for(int i = 1; i <= W; ++i){
		iii front = singole.top(); singole.pop();
		ans+= front.first;
		//~ cout << "Considering " << i << " single stars " << " with time " << ans << endl;
		if(state[front.second.second] == 0) remove(front.second.second); // Rimuove dal bit che esiste coppia in posizione i
		if(state[front.second.second] == 0) singole.push( g(front.second.first-front.first,BIG,front.second.second));
		//~ cout << "Pushing " << front.second.first - front.first <<  " , " << BIG << " , " << i << endl;
		ll val = compute( (W-i) / 2 + ((W - i)%2));
		//~ cout << "Have to add " << val << " time" << endl << endl;
		if(best > ans + val){
			copia = ok.ft;
			bestpos = i;
			best = ans + val;
		}
		state[front.second.second]++;
	}
	cout << best << endl;
	int q = ( W - bestpos)/2 + ((W - bestpos)%2);
	int pos = 1;
	ok.ft = copia;
	while(q){
		if(ok.rsq(pos,pos) == 1){
			state[pos] = 2; --q;}
		++pos;
	}
	for(int i = 1; i <= N; ++i)
		cout << state[i];
	cout << endl;
}


#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define LSOne(S)(S & (-S))
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
vi copia;
int N,W;
const ll BIG = 999999999999;
int state[300005];
int stateok[300005];
int O[300005];
int A[300005];
int B[300005];
int C[300005];
class FenwickTree{
	public:vi ft;
	FenwickTree(){ ft.assign(300005+1,0);}

	ll rsq(int b){
		ll sum = 0; for(;b;b-=LSOne(b)) sum+= ft[b]; return sum;}

	ll rsq(int a, int b){
		return rsq(b) - (a == 1 ? 0 : rsq(a-1));}

	void adjust(int k, int v){
		for(; k < (int)ft.size(); k+= LSOne(k)) ft[k] += v;}
};

FenwickTree ok;
FenwickTree quanto;

void remove(int pos){
	ok.adjust(C[pos], -1 );
	quanto.adjust(C[pos], -B[O[C[pos]]] );
}

ll compute(int a){
	if(a==0) return 0;
	//~ cout << "Computing " << a << endl;
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << "i :" <<  quanto.rsq(i,i) <<  " " << ok.rsq(i,i) << endl;
	int lo = 1; int hi = N;
	while(lo + 1 < hi){
		int mid = (lo+hi)/2;
		if(ok.rsq(mid) <= a)
			lo = mid;
		else hi = mid-1;
	}
	//~ cout << lo << " " << hi << endl;
	ll a1, a2;
	if(ok.rsq((hi)) < a) a1 = BIG; else a1 = quanto.rsq(hi);
	if(ok.rsq((lo)) < a) a2 = BIG; else a2 = quanto.rsq(lo);
	if(min(a1,a2) < 0) cout << "CIAO MAMMA" << endl;
	return min(a1,a2);
}

typedef pair<ll,int> ii;
typedef pair<int,ii> iii;
ll ans;
priority_queue< iii, vector<iii>, greater<iii> > singole;
iii g(int a,ll b, int c){
	iii temp; temp.first = a; temp.second.first = b; temp.second.second = c; return temp;
}

bool compare(int x, int y){
	//~ cout << "B[" << a << "] = " << B[a] << ", ";
	//~ cout << "B[" << b << "] = " << B[b] << endl;
	return B[x] <= B[y];
}

int main()
{
	//~ freopen("input.txt","r",stdin);

	cin >> N >> W;
	for(int i = 1; i <= N; ++i)
		state[i] = 0;
	for(int i = 1; i <= N; ++i)
		cin >> A[i]  >> B[i];

	for(int i = 1; i <= N; ++i){
		singole.push( g(A[i],B[i],i));
	}

	for(int i = 1; i <= N; ++i)
		O[i] = i;

	//~ for(int i = 1; i <= N; ++i)
		//~ cout << A[i] << "    ";
	//~ cout << endl;
	//~
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << B[i] << "    ";
	//~ cout << endl;

	sort(O+1,O+N+1,compare);

	for(int i = 1; i <= N; ++i){
		ok.adjust(i,1);
		quanto.adjust(i,B[O[i]]);
	}
	for(int i = 1; i<=N; ++i)
		C[O[i]] = i;
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << C[i] << "  ";
	//~ cout << endl;

	ans = 0;
	ll best = 999999999999;
	int bestpos = 0;
	for(int i = 1; i <= W; ++i){
		iii front = singole.top(); singole.pop();
		ans+= front.first;
		//~ cout << "Considering " << i << " single stars " << " with time " << ans << endl;
		if(state[front.second.second] == 0) remove(front.second.second); // Rimuove dal bit che esiste coppia in posizione i
		if(state[front.second.second] == 0) singole.push( g(front.second.first-front.first,BIG,front.second.second));
		//~ cout << "Pushing " << front.second.first - front.first <<  " , " << BIG << " , " << i << endl;
		ll val = compute( (W-i) / 2 + ((W - i)%2));
		//~ cout << "Have to add " << val << " time" << endl << endl;
		state[front.second.second]++;
		if(best > ans + val){
			copia = ok.ft;
			bestpos = i;
			best = ans + val;
			memcpy(stateok,state,sizeof(state));
		}
	}
	cout << best << endl;
	int q = ( W - bestpos)/2 + ((W - bestpos)%2);
	//cout << q << endl;
	int pos = 1;
	ok.ft = copia;

	while(q){
		if(ok.rsq(pos,pos) == 1){
			stateok[C[pos]] = 2; --q;}
		++pos;
	}
	for(int i = 1; i <= N; ++i)
		cout << stateok[i];
	cout << endl;
}


#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define LSOne(S)(S & (-S))
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
vi copia;
int N,W;
const ll BIG = 9999999999;
int state[300005];
int stateok[300005];
int O[300005];
int A[300005];
int B[300005];
int C[300005];
class FenwickTree{
	public:vi ft;
	FenwickTree(){ ft.assign(300005+1,0);}

	ll rsq(int b){
		int sum = 0; for(;b;b-=LSOne(b)) sum+= ft[b]; return sum;}

	ll rsq(int a, int b){
		return rsq(b) - (a == 1 ? 0 : rsq(a-1));}

	void adjust(int k, int v){
		for(; k < (int)ft.size(); k+= LSOne(k)) ft[k] += v;}
};

FenwickTree ok;
FenwickTree quanto;

void remove(int pos){
	ok.adjust(C[pos], -1 );
	quanto.adjust(C[pos], -B[O[C[pos]]] );
}

ll compute(int a){
	if(a==0) return 0;
	//~ cout << "Computing " << a << endl;
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << "i :" <<  quanto.rsq(i,i) <<  " " << ok.rsq(i,i) << endl;
	int lo = 1; int hi = N;
	while(lo + 1 < hi){
		int mid = (lo+hi)/2;
		if(ok.rsq(mid) <= a)
			lo = mid;
		else hi = mid-1;
	}
	//~ cout << lo << " " << hi << endl;
	ll a1, a2;
	if(ok.rsq((hi)) < a) a1 = BIG; else a1 = quanto.rsq(hi);
	if(ok.rsq((lo)) < a) a2 = BIG; else a2 = quanto.rsq(lo);

	return min(a1,a2);
}

typedef pair<int,int> ii;
typedef pair<int,ii> iii;
ll ans;
priority_queue< iii, vector<iii>, greater<iii> > singole;
iii g(int a,int b, int c){
	iii temp; temp.first = a; temp.second.first = b; temp.second.second = c; return temp;
}

bool compare(int x, int y){
	//~ cout << "B[" << a << "] = " << B[a] << ", ";
	//~ cout << "B[" << b << "] = " << B[b] << endl;
	return B[x] <= B[y];
}

int main()
{
	//~ freopen("input.txt","r",stdin);

	cin >> N >> W;
	for(int i = 1; i <= N; ++i)
		state[i] = 0;
	for(int i = 1; i <= N; ++i)
		cin >> A[i]  >> B[i];

	for(int i = 1; i <= N; ++i){
		singole.push( g(A[i],B[i],i));
	}

	for(int i = 1; i <= N; ++i)
		O[i] = i;

	//~ for(int i = 1; i <= N; ++i)
		//~ cout << A[i] << "    ";
	//~ cout << endl;
	//~
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << B[i] << "    ";
	//~ cout << endl;

	sort(O+1,O+N+1,compare);

	for(int i = 1; i <= N; ++i){
		ok.adjust(i,1);
		quanto.adjust(i,B[O[i]]);
	}
	for(int i = 1; i<=N; ++i)
		C[O[i]] = i;
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << C[i] << "  ";
	//~ cout << endl;

	ans = 0;
	ll best = 99999999;
	int bestpos = 0;
	for(int i = 1; i <= W; ++i){
		iii front = singole.top(); singole.pop();
		ans+= front.first;
		//~ cout << "Considering " << i << " single stars " << " with time " << ans << endl;
		if(state[front.second.second] == 0) remove(front.second.second); // Rimuove dal bit che esiste coppia in posizione i
		if(state[front.second.second] == 0) singole.push( g(front.second.first-front.first,BIG,front.second.second));
		//~ cout << "Pushing " << front.second.first - front.first <<  " , " << BIG << " , " << i << endl;
		ll val = compute( (W-i) / 2 + ((W - i)%2));
		//~ cout << "Have to add " << val << " time" << endl << endl;
		state[front.second.second]++;
		if(best > ans + val){
			copia = ok.ft;
			bestpos = i;
			best = ans + val;
			memcpy(stateok,state,sizeof(state));
		}
	}
	cout << best << endl;
	int q = ( W - bestpos)/2 + ((W - bestpos)%2);
	//cout << q << endl;
	int pos = 1;
	ok.ft = copia;

	while(q){
		if(ok.rsq(pos,pos) == 1){
			stateok[C[pos]] = 2; --q;}
		++pos;
	}
	for(int i = 1; i <= N; ++i)
		cout << stateok[i];
	cout << endl;
}


#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define LSOne(S)(S & (-S))
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
vi copia;
int N,W;
const ll BIG = 999999999999;
int state[300005];
int stateok[300005];
int O[300005];
int A[300005];
int B[300005];
int C[300005];
class FenwickTree{
	public:vi ft;
	FenwickTree(){ ft.assign(300005+1,0);}

	ll rsq(int b){
		ll sum = 0; for(;b;b-=LSOne(b)) sum+= ft[b]; return sum;}

	ll rsq(int a, int b){
		return rsq(b) - (a == 1 ? 0 : rsq(a-1));}

	void adjust(int k, int v){
		for(; k < (int)ft.size(); k+= LSOne(k)) ft[k] += v;}
};

FenwickTree ok;
FenwickTree quanto;

void remove(int pos){
	ok.adjust(C[pos], -1 );
	quanto.adjust(C[pos], -B[O[C[pos]]] );
}

ll compute(int a){
	if(a==0) return 0;
	//~ cout << "Computing " << a << endl;
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << "i :" <<  quanto.rsq(i,i) <<  " " << ok.rsq(i,i) << endl;
	int lo = 1; int hi = N;
	while(lo + 1 < hi){
		int mid = (lo+hi)/2;
		if(ok.rsq(mid) <= a)
			lo = mid;
		else hi = mid-1;
	}
	//~ cout << lo << " " << hi << endl;
	ll a1, a2;
	if(ok.rsq((hi)) < a) a1 = BIG; else a1 = quanto.rsq(hi);
	if(ok.rsq((lo)) < a) a2 = BIG; else a2 = quanto.rsq(lo);
	return max(min(a1,a2),1ll*0);
}

typedef pair<ll,int> ii;
typedef pair<int,ii> iii;
ll ans;
priority_queue< iii, vector<iii>, greater<iii> > singole;
iii g(int a,ll b, int c){
	iii temp; temp.first = a; temp.second.first = b; temp.second.second = c; return temp;
}

bool compare(int x, int y){
	//~ cout << "B[" << a << "] = " << B[a] << ", ";
	//~ cout << "B[" << b << "] = " << B[b] << endl;
	return B[x] <= B[y];
}

int main()
{
	freopen("input.txt","r",stdin);

	cin >> N >> W;
	for(int i = 1; i <= N; ++i)
		state[i] = 0;
	for(int i = 1; i <= N; ++i)
		cin >> A[i]  >> B[i];

	for(int i = 1; i <= N; ++i){
		singole.push( g(A[i],B[i],i));
	}

	for(int i = 1; i <= N; ++i)
		O[i] = i;

	//~ for(int i = 1; i <= N; ++i)
		//~ cout << A[i] << "    ";
	//~ cout << endl;
	//~
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << B[i] << "    ";
	//~ cout << endl;

	sort(O+1,O+N+1,compare);

	for(int i = 1; i <= N; ++i){
		ok.adjust(i,1);
		quanto.adjust(i,B[O[i]]);
	}
	for(int i = 1; i<=N; ++i)
		C[O[i]] = i;
	//~ for(int i = 1; i <= N; ++i)
		//~ cout << C[i] << "  ";
	//~ cout << endl;

	ans = 0;
	ll best = 999999999999;
	int bestpos = 0;
	for(int i = 1; i <= W; ++i){
		iii front = singole.top(); singole.pop();
		ans+= front.first;
		//~ cout << "Considering " << i << " single stars " << " with time " << ans << endl;
		if(state[front.second.second] == 0) remove(front.second.second); // Rimuove dal bit che esiste coppia in posizione i
		if(state[front.second.second] == 0) singole.push( g(front.second.first-front.first,BIG,front.second.second));
		//~ cout << "Pushing " << front.second.first - front.first <<  " , " << BIG << " , " << i << endl;
		ll val = compute( (W-i) / 2 + ((W - i)%2));
		//~ cout << "Have to add " << val << " time" << endl << endl;
		state[front.second.second]++;
		if(best > ans + val){
			copia = ok.ft;
			bestpos = i;
			best = ans + val;
			memcpy(stateok,state,sizeof(state));
		}
	}
	cout << best << endl;
	int q = ( W - bestpos)/2 + ((W - bestpos)%2);
	//cout << q << endl;
	int pos = 1;
	ok.ft = copia;

	while(q){
		if(ok.rsq(pos,pos) == 1){
			stateok[C[pos]] = 2; --q;}
		++pos;
	}
	for(int i = 1; i <= N; ++i)
		cout << stateok[i];
	cout << endl;
}


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <bitset>

using namespace std;

typedef long long int64;

const int N = 300030;
const int INF = 2e9;

struct onet {
	int w, id;
} one[N];

int n, w, a[N], b[N], lst[N], onep, ct[N], id[N];
int maxb[N], maxab[N], mina[N];
int64 sum[N];

bool cmp_o(const onet& a, const onet& b) {
	return a.w < b.w;
}
bool cmp_lst(const int& i, const int& j) {
	return a[i] + b[i] < a[j] + b[j];
}
int64 choose(int w, bool bt) {
	if (w % 2 == 0) {
		if (bt) {
			for (int i = 1; i <= w / 2; ++i)
				ct[id[lst[i]]] = 2;
		}
		return sum[w / 2];
	}
	int t = (w + 1) / 2;
	int64 d1 = maxb[t];
	int64 d2 = maxab[t] - mina[t + 1];

	if (bt) {
		for (int i = 1; i <= t; ++i)
			ct[id[lst[i]]] = 2;
		if (d1 > d2) {
			for (int ii = 1; ii <= t; ++ii) {
				int i = lst[ii];
				if (b[i] == maxb[t]) {
					--ct[id[i]];
					break;
				}
			}
		} else {
			for (int ii = 1; ii <= t; ++ii) {
				int i = lst[ii];
				if (a[i] + b[i] == maxab[t]) {
					ct[id[i]] = 0;
					break;
				}
			}
			for (int ii = t + 1; ii <= n; ++ii) {
				int i = lst[ii];
				if (a[i] == mina[t + 1]) {
					++ct[id[i]];
					break;
				}
			}
		}
	}
	return sum[t] - max(d1, d2);
}
int main() {
#ifndef ONLINE_JUDGE
	freopen("cf.in" , "r", stdin);
	freopen("cf.out", "w", stdout);
#endif
	scanf("%d %d", &n, &w);
	int on = n, nn = 0;
	for (int i = 1; i <= n; ++i) {
		scanf("%d %d", a + i, b + i);
		b[i] -= a[i];
		if (a[i] <= b[i]) {
			one[++onep] = (onet) {a[i], i};
			one[++onep] = (onet) {b[i], i};
		} else {
			a[++nn] = a[i], b[nn] = b[i];
			id[nn] = i;
		}
	}
	n = nn;
	sort(one + 1, one + onep + 1, cmp_o);

	for (int i = 1; i <= n; ++i)
		lst[i] = i;
	sort(lst + 1, lst + n + 1, cmp_lst);

	for (int ii = 1, i; i = lst[ii], ii <= n; ++ii) {
		sum[i] = sum[i - 1] + a[i] + b[i];
	}
	maxb[0] = -INF, maxab[0] = 0;
	for (int ii = 1; ii <= n; ++ii) {
		maxb[ii] = max(maxb[ii - 1], b[lst[ii]]);
		maxab[ii] = max(maxab[ii - 1], a[lst[ii]] + b[lst[ii]]);
	}
	mina[n + 1] = INF;
	for (int ii = n; ii >= 1; --ii)
		mina[ii] = min(mina[ii + 1], a[lst[ii]]);

	int64 ans = 1e18;
	int bst = 0;

	int64 cur = 0;

	for (int i = 0; i <= onep; ++i) {
		cur += one[i].w;
		if (w - i < 0 || w - i > n * 2)
			continue;
		int64 tmp = cur + choose(w - i, 0);
		if (tmp < ans) {
			ans = tmp;
			bst = i;
		}
	}

	cout << ans << endl;
	for (int i = 1; i <= bst; ++i)
		++ct[one[i].id];
	choose(w - bst, 1);
	for (int i = 1; i <= on; ++i)
		printf("%d", ct[i]);
}


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <bitset>

using namespace std;

typedef long long int64;

const int N = 300030;
const int INF = 2e9;

struct onet {
	int w, id;
} one[N];

int n, w, a[N], b[N], lst[N], onep, ct[N], id[N];
int maxb[N], maxab[N], mina[N];
int64 sum[N];

bool cmp_o(const onet& a, const onet& b) {
	return a.w < b.w;
}
bool cmp_lst(const int& i, const int& j) {
	return a[i] + b[i] < a[j] + b[j];
}
int64 choose(int w, bool bt) {
	if (w % 2 == 0) {
		if (bt) {
			for (int i = 1; i <= w / 2; ++i)
				ct[id[lst[i]]] = 2;
		}
		return sum[w / 2];
	}
	int t = (w + 1) / 2;
	int64 d1 = maxb[t];
	int64 d2 = maxab[t] - mina[t + 1];

	if (bt) {
		for (int i = 1; i <= t; ++i)
			ct[id[lst[i]]] = 2;
		if (d1 > d2) {
			for (int ii = 1; ii <= t; ++ii) {
				int i = lst[ii];
				if (b[i] == maxb[t]) {
					--ct[id[i]];
					break;
				}
			}
		} else {
			for (int ii = 1; ii <= t; ++ii) {
				int i = lst[ii];
				if (a[i] + b[i] == maxab[t]) {
					ct[id[i]] = 0;
					break;
				}
			}
			for (int ii = t + 1; ii <= n; ++ii) {
				int i = lst[ii];
				if (a[i] == mina[t + 1]) {
					++ct[id[i]];
					break;
				}
			}
		}
	}
	return sum[t] - max(d1, d2);
}
int main() {
#ifndef ONLINE_JUDGE
	freopen("cf.in" , "r", stdin);
	freopen("cf.out", "w", stdout);
#endif
	scanf("%d %d", &n, &w);
	int on = n, nn = 0;
	for (int i = 1; i <= n; ++i) {
		scanf("%d %d", a + i, b + i);
		b[i] -= a[i];
		if (a[i] <= b[i]) {
			one[++onep] = (onet) {a[i], i};
			one[++onep] = (onet) {b[i], i};
		} else {
			a[++nn] = a[i], b[nn] = b[i];
			id[nn] = i;
		}
	}
	n = nn;
	sort(one + 1, one + onep + 1, cmp_o);

	for (int i = 1; i <= n; ++i)
		lst[i] = i;
	sort(lst + 1, lst + n + 1, cmp_lst);

	for (int ii = 1, i; i = lst[ii], ii <= n; ++ii) {
		sum[i] = sum[i - 1] + a[i] + b[i];
	}
	maxb[0] = -INF, maxab[0] = 0;
	for (int ii = 1; ii <= n; ++ii) {
		maxb[ii] = max(maxb[ii - 1], b[lst[ii]]);
		maxab[ii] = max(maxab[ii - 1], a[lst[ii]] + b[lst[ii]]);
	}
	mina[n + 1] = INF;
	for (int ii = n; ii >= 1; --ii)
		mina[ii] = min(mina[ii + 1], a[lst[ii]]);

	int64 ans = 1e18;
	int bst = 0;

	int64 cur = 0;

	for (int i = 0; i <= onep; ++i) {
		cur += one[i].w;
		if (w - i < 0 || w - i > n)
			continue;
		int64 tmp = cur + choose(w - i, 0);
		if (tmp < ans) {
			ans = tmp;
			bst = i;
		}
	}

	cout << ans << endl;
	for (int i = 1; i <= bst; ++i)
		++ct[one[i].id];
	choose(w - bst, 1);
	for (int i = 1; i <= on; ++i)
		printf("%d", ct[i]);
}


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <bitset>

using namespace std;

typedef long long int64;

const int N = 600060;
const int INF = 2e9;

struct onet {
	int w, id;
} one[N];

int n, w, a[N], b[N], lst[N], onep, ct[N], id[N];
int maxb[N], maxab[N], mina[N];
int64 sum[N];

bool cmp_o(const onet& a, const onet& b) {
	return a.w < b.w;
}
bool cmp_lst(const int& i, const int& j) {
	return a[i] + b[i] < a[j] + b[j];
}
int64 choose(int w, bool bt) {
	if (w % 2 == 0) {
		if (bt) {
			for (int i = 1; i <= w / 2; ++i)
				ct[id[lst[i]]] = 2;
		}
		return sum[w / 2];
	}
	int t = (w + 1) / 2;
	int64 d1 = maxb[t];
	int64 d2 = maxab[t] - mina[t + 1];

	if (bt) {
		for (int i = 1; i <= t; ++i)
			ct[id[lst[i]]] = 2;
		if (d1 > d2) {
			for (int ii = 1; ii <= t; ++ii) {
				int i = lst[ii];
				if (b[i] == maxb[t]) {
					--ct[id[i]];
					break;
				}
			}
		} else {
			for (int ii = 1; ii <= t; ++ii) {
				int i = lst[ii];
				if (a[i] + b[i] == maxab[t]) {
					ct[id[i]] = 0;
					break;
				}
			}
			for (int ii = t + 1; ii <= n; ++ii) {
				int i = lst[ii];
				if (a[i] == mina[t + 1]) {
					++ct[id[i]];
					break;
				}
			}
		}
	}
	return sum[t] - max(d1, d2);
}
int main() {
#ifndef ONLINE_JUDGE
	freopen("cf.in" , "r", stdin);
	freopen("cf.out", "w", stdout);
#endif
	scanf("%d %d", &n, &w);
	int on = n, nn = 0;
	for (int i = 1; i <= n; ++i) {
		scanf("%d %d", a + i, b + i);
		b[i] -= a[i];
		if (a[i] <= b[i]) {
			one[++onep] = (onet) {a[i], i};
			one[++onep] = (onet) {b[i], i};
		} else {
			a[++nn] = a[i], b[nn] = b[i];
			id[nn] = i;
		}
	}
	n = nn;
	sort(one + 1, one + onep + 1, cmp_o);

	for (int i = 1; i <= n; ++i)
		lst[i] = i;
	sort(lst + 1, lst + n + 1, cmp_lst);

	for (int ii = 1, i; i = lst[ii], ii <= n; ++ii) {
		sum[ii] = sum[ii - 1] + a[i] + b[i];
	}
	maxb[0] = -INF, maxab[0] = 0;
	for (int ii = 1; ii <= n; ++ii) {
		maxb[ii] = max(maxb[ii - 1], b[lst[ii]]);
		maxab[ii] = max(maxab[ii - 1], a[lst[ii]] + b[lst[ii]]);
	}
	mina[n + 1] = INF;
	for (int ii = n; ii >= 1; --ii)
		mina[ii] = min(mina[ii + 1], a[lst[ii]]);

	int64 ans = 1e18;
	int bst = 0;

	int64 cur = 0;

	for (int i = 0; i <= onep; ++i) {
		cur += one[i].w;
		if (w - i < 0 || w - i > n * 2)
			continue;
		if (i == 11)
			i = 11;
		int64 tmp = cur + choose(w - i, 0);
		if (tmp < ans) {
			ans = tmp;
			bst = i;
		}
	}

	cout << ans << endl;
	for (int i = 1; i <= bst; ++i)
		++ct[one[i].id];
	choose(w - bst, 1);
	for (int i = 1; i <= on; ++i)
		printf("%d", ct[i]);
}


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <bitset>

using namespace std;

typedef long long int64;

const int N = 300030;
const int INF = 2e9;

struct onet {
	int w, id;
} one[N];

int n, w, a[N], b[N], lst[N], onep, ct[N], id[N];
int maxb[N], maxab[N], mina[N];
int64 sum[N];

bool cmp_o(const onet& a, const onet& b) {
	return a.w < b.w;
}
bool cmp_lst(const int& i, const int& j) {
	return a[i] + b[i] < a[j] + b[j];
}
int64 choose(int w, bool bt) {
	if (w % 2 == 0) {
		if (bt) {
			for (int i = 1; i <= w / 2; ++i)
				ct[id[lst[i]]] = 2;
		}
		return sum[w / 2];
	}
	int t = (w + 1) / 2;
	int64 d1 = maxb[t];
	int64 d2 = maxab[t] - mina[t + 1];

	if (bt) {
		for (int i = 1; i <= t; ++i)
			ct[id[lst[i]]] = 2;
		if (d1 > d2) {
			for (int ii = 1; ii <= t; ++ii) {
				int i = lst[ii];
				if (b[i] == maxb[t]) {
					--ct[id[i]];
					break;
				}
			}
		} else {
			for (int ii = 1; ii <= t; ++ii) {
				int i = lst[ii];
				if (a[i] + b[i] == maxab[t]) {
					ct[id[i]] = 0;
					break;
				}
			}
			for (int ii = t + 1; ii <= n; ++ii) {
				int i = lst[ii];
				if (a[i] == mina[t + 1]) {
					++ct[id[i]];
					break;
				}
			}
		}
	}
	return sum[t] - max(d1, d2);
}
int main() {
#ifndef ONLINE_JUDGE
	freopen("cf.in" , "r", stdin);
	freopen("cf.out", "w", stdout);
#endif
	scanf("%d %d", &n, &w);
	int on = n, nn = 0;
	for (int i = 1; i <= n; ++i) {
		scanf("%d %d", a + i, b + i);
		b[i] -= a[i];
		if (a[i] <= b[i]) {
			one[++onep] = (onet) {a[i], i};
			one[++onep] = (onet) {b[i], i};
		} else {
			a[++nn] = a[i], b[nn] = b[i];
			id[nn] = i;
		}
	}
	n = nn;
	sort(one + 1, one + onep + 1, cmp_o);

	for (int i = 1; i <= n; ++i)
		lst[i] = i;
	sort(lst + 1, lst + n + 1, cmp_lst);

	for (int ii = 1, i; i = lst[ii], ii <= n; ++ii) {
		sum[ii] = sum[ii - 1] + a[i] + b[i];
	}
	maxb[0] = -INF, maxab[0] = 0;
	for (int ii = 1; ii <= n; ++ii) {
		maxb[ii] = max(maxb[ii - 1], b[lst[ii]]);
		maxab[ii] = max(maxab[ii - 1], a[lst[ii]] + b[lst[ii]]);
	}
	mina[n + 1] = INF;
	for (int ii = n; ii >= 1; --ii)
		mina[ii] = min(mina[ii + 1], a[lst[ii]]);

	int64 ans = 1e18;
	int bst = 0;

	int64 cur = 0;

	for (int i = 0; i <= onep; ++i) {
		cur += one[i].w;
		if (w - i < 0 || w - i > n * 2)
			continue;
		if (i == 11)
			i = 11;
		int64 tmp = cur + choose(w - i, 0);
		if (tmp < ans) {
			ans = tmp;
			bst = i;
		}
	}

	cout << ans << endl;
	for (int i = 1; i <= bst; ++i)
		++ct[one[i].id];
	choose(w - bst, 1);
	for (int i = 1; i <= on; ++i)
		printf("%d", ct[i]);
}


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <bitset>

using namespace std;

typedef long long int64;

const int N = 300030;
const int INF = 2e9;

struct onet {
	int w, id;
} one[N];

int n, w, a[N], b[N], lst[N], onep, ct[N], id[N];
int maxb[N], maxab[N], mina[N];
int64 sum[N];

bool cmp_o(const onet& a, const onet& b) {
	return a.w < b.w;
}
bool cmp_lst(const int& i, const int& j) {
	return a[i] + b[i] < a[j] + b[j];
}
int64 choose(int w, bool bt) {
	if (w % 2 == 0) {
		if (bt) {
			for (int i = 1; i <= w / 2; ++i)
				ct[id[lst[i]]] = 2;
		}
		return sum[w / 2];
	}
	int t = (w + 1) / 2;
	int64 d1 = maxb[t];
	int64 d2 = maxab[t] - mina[t + 1];

	if (bt) {
		for (int i = 1; i <= t; ++i)
			ct[id[lst[i]]] = 2;
		if (d1 < d2) {
			for (int ii = 1; ii <= t; ++ii) {
				int i = lst[ii];
				if (b[i] == maxb[t]) {
					--ct[id[i]];
					break;
				}
			}
		} else {
			for (int ii = 1; ii <= t; ++ii) {
				int i = lst[ii];
				if (a[i] + b[i] == maxab[t]) {
					ct[id[i]] = 0;
					break;
				}
			}
			for (int ii = t + 1; ii <= n; ++ii) {
				int i = lst[ii];
				if (a[i] == mina[t + 1]) {
					++ct[id[i]];
					break;
				}
			}
		}
	}
	return sum[t] - max(d1, d2);
}
int main() {
#ifndef ONLINE_JUDGE
	freopen("cf.in" , "r", stdin);
	freopen("cf.out", "w", stdout);
#endif
	scanf("%d %d", &n, &w);
	int on = n, nn = 0;
	for (int i = 1; i <= n; ++i) {
		scanf("%d %d", a + i, b + i);
		b[i] -= a[i];
		if (a[i] <= b[i]) {
			one[++onep] = (onet) {a[i], i};
			one[++onep] = (onet) {b[i], i};
		} else {
			a[++nn] = a[i], b[nn] = b[i];
			id[nn] = i;
		}
	}
	n = nn;
	sort(one + 1, one + onep + 1, cmp_o);

	for (int i = 1; i <= n; ++i)
		lst[i] = i;
	sort(lst + 1, lst + n + 1, cmp_lst);

	for (int ii = 1, i; i = lst[ii], ii <= n; ++ii) {
		sum[i] = sum[i - 1] + a[i] + b[i];
	}
	maxb[0] = -INF, maxab[0] = 0;
	for (int ii = 1; ii <= n; ++ii) {
		maxb[ii] = max(maxb[ii - 1], b[lst[ii]]);
		maxab[ii] = max(maxab[ii - 1], a[lst[ii]] + b[lst[ii]]);
	}
	mina[n + 1] = INF;
	for (int ii = n; ii >= 1; --ii)
		mina[ii] = min(mina[ii + 1], a[lst[ii]]);

	int64 ans = 1e18;
	int bst = 0;

	int64 cur = 0;

	for (int i = 0; i <= onep; ++i) {
		cur += one[i].w;
		if (w - i < 0 || w - i > n)
			continue;
		int64 tmp = cur + choose(w - i, 0);
		if (tmp < ans) {
			ans = tmp;
			bst = i;
		}
	}

	cout << ans << endl;
	for (int i = 1; i <= bst; ++i)
		++ct[one[i].id];
	choose(w - bst, 1);
	for (int i = 1; i <= on; ++i)
		printf("%d", ct[i]);
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
#define rep(i,n) for (int i=0;i<n;++i)
const int N=200005;
int n,m,cnt,L,a[N],b[N],c[N],d[N],p[N],q[N],o[N]; LL sum,ans;
bool cmp(const int i,const int j){return a[i]==a[j]?(i&1)==(j&1)?i<j:i&1:a[i]<a[j];}
int main()
{
	scanf("%d%d",&n,&m);
	rep(i,n){
		scanf("%d%d",a+i,b+i),a[i]*=2,b[i]*=2,c[i]=a[i],a[n+i]=b[i]-a[i];
		if (b[i]/2<a[i]) a[i]=a[n+i]=b[i]/2;
	}
	rep(i,2*n) p[i]=i; sort(p,p+2*n,cmp); rep(i,m) d[p[i]]=1;
	rep(i,m) ans+=a[p[i]],++o[p[i]<n?p[i]:p[i]-n];
	int x=p[m-1],y=x;
	if (x<n && !d[x+n] && c[x]!=a[x]){
		sum=ans-a[x],ans=sum+c[x],y=x,--o[x];
		rep(i,n) if (d[i] && !d[i+n] && a[i]==c[i])
			if (sum+a[i+n]<ans) ans=sum+a[i+n],y=i;
		rep(i,n) if (d[i] && d[i+n] && a[i]==c[i])
			if (sum-a[i+n]+a[x]+a[x+n]<ans) ans=sum-a[i+n]+a[x]+a[x+n],y=-i-1;
		rep(i,n) if (!d[i]) if (sum+c[i]<ans) ans=sum+c[i],y=i;
		if (y<0) y=-1-y,--o[y],o[x]+=2; else ++o[y<n?y:y-n];
	}
	printf("%I64d\n",ans/2);
	rep(i,n) printf("%d",o[i]);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
#define rep(i,n) for (int i=0;i<n;++i)
const int N=600005;
int n,m,cnt,L,a[N],b[N],c[N],d[N],p[N],q[N],o[N],rk[N]; LL sum,ans;
bool cmp(const int i,const int j){return a[i]==a[j]?rk[i]<rk[j]:a[i]<a[j];}
int main()
{
	scanf("%d%d",&n,&m);
	rep(i,n){
		scanf("%d%d",a+i,b+i),a[i]*=2,b[i]*=2,c[i]=a[i],a[n+i]=b[i]-a[i];
		if (b[i]/2<a[i]) a[i]=a[n+i]=b[i]/2; rk[i]=i*2,rk[i+n]=i*2+1;
	}
	rep(i,2*n) p[i]=i; sort(p,p+2*n,cmp); rep(i,m) d[p[i]]=1;
	//rep(i,m) printf("%d ",p[i]); puts("");
	rep(i,m) ans+=a[p[i]],++o[p[i]<n?p[i]:p[i]-n];
	int x=p[m-1],y=x;
	if (x<n && !d[x+n] && c[x]!=a[x]){
		sum=ans-a[x],ans=sum+c[x],y=x,--o[x];
		rep(i,n) if (i!=x && d[i] && !d[i+n] && a[i]==c[i]){
			if (sum+a[i+n]<ans) ans=sum+a[i+n],y=i;
			if (sum-a[i]+a[x]+a[x+n]<ans) ans=sum-a[i]+a[x]+a[x+n],y=-i-1;
		}
		rep(i,n) if (i!=x && d[i] && d[i+n] && a[i]==c[i])
			if (sum-a[i+n]+a[x]+a[x+n]<ans) ans=sum-a[i+n]+a[x]+a[x+n],y=-i-1;
		rep(i,n) if (!d[i]) if (sum+c[i]<ans) ans=sum+c[i],y=i;
		if (y<0) y=-1-y,--o[y],o[x]+=2; else ++o[y<n?y:y-n];
	}
	printf("%I64d\n",ans/2);
	rep(i,n) printf("%d",o[i]);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
#define rep(i,n) for (int i=0;i<n;++i)
const int N=600005;
int n,m,cnt,L,a[N],b[N],c[N],d[N],p[N],q[N],o[N]; LL sum,ans;
bool cmp(const int i,const int j){return a[i]==a[j]?(i&1)==(j&1)?i<j:i&1:a[i]<a[j];}
int main()
{
	scanf("%d%d",&n,&m);
	rep(i,n){
		scanf("%d%d",a+i,b+i),a[i]*=2,b[i]*=2,c[i]=a[i],a[n+i]=b[i]-a[i];
		if (b[i]/2<a[i]) a[i]=a[n+i]=b[i]/2;
	}
	rep(i,2*n) p[i]=i; sort(p,p+2*n,cmp); rep(i,m) d[p[i]]=1;
	rep(i,m) ans+=a[p[i]],++o[p[i]<n?p[i]:p[i]-n];
	int x=p[m-1],y=x;
	if (x<n && !d[x+n] && c[x]!=a[x]){
		sum=ans-a[x],ans=sum+c[x],y=x,--o[x];
		rep(i,n) if (d[i] && !d[i+n] && a[i]==c[i])
			if (sum+a[i+n]<ans) ans=sum+a[i+n],y=i;
		rep(i,n) if (d[i] && d[i+n] && a[i]==c[i])
			if (sum-a[i+n]+a[x]+a[x+n]<ans) ans=sum-a[i+n]+a[x]+a[x+n],y=-i-1;
		rep(i,n) if (!d[i]) if (sum+c[i]<ans) ans=sum+c[i],y=i;
		if (y<0) y=-1-y,--o[y],o[x]+=2; else ++o[y<n?y:y-n];
	}
	printf("%I64d\n",ans/2);
	rep(i,n) printf("%d",o[i]);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
#define rep(i,n) for (int i=0;i<n;++i)
const int N=600005;
int n,m,cnt,L,a[N],b[N],c[N],d[N],p[N],q[N],o[N]; LL sum,ans;
bool cmp(const int i,const int j){return a[i]==a[j]?(i&1)==(j&1)?i<j:i&1:a[i]<a[j];}
int main()
{
	scanf("%d%d",&n,&m);
	rep(i,n){
		scanf("%d%d",a+i,b+i),a[i]*=2,b[i]*=2,c[i]=a[i],a[n+i]=b[i]-a[i];
		if (b[i]/2<a[i]) a[i]=a[n+i]=b[i]/2;
	}
	rep(i,2*n) p[i]=i; sort(p,p+2*n,cmp); rep(i,m) d[p[i]]=1;
	//rep(i,m) printf("%d ",p[i]); puts("");
	rep(i,m) ans+=a[p[i]],++o[p[i]<n?p[i]:p[i]-n];
	int x=p[m-1],y=x;
	if (x<n && !d[x+n] && c[x]!=a[x]){
		sum=ans-a[x],ans=sum+c[x],y=x,--o[x];
		rep(i,n) if (d[i] && !d[i+n] && a[i]==c[i]){
			if (sum+a[i+n]<ans) ans=sum+a[i+n],y=i;
			if (sum-a[i]+a[x]+a[x+n]<ans) ans=sum-a[i]+a[x]+a[x+n],y=-i-1;
		}
		rep(i,n) if (d[i] && d[i+n] && a[i]==c[i])
			if (sum-a[i+n]+a[x]+a[x+n]<ans) ans=sum-a[i+n]+a[x]+a[x+n],y=-i-1;
		rep(i,n) if (!d[i]) if (sum+c[i]<ans) ans=sum+c[i],y=i;
		if (y<0) y=-1-y,--o[y],o[x]+=2; else ++o[y<n?y:y-n];
	}
	printf("%I64d\n",ans/2);
	rep(i,n) printf("%d",o[i]);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
#define rep(i,n) for (int i=0;i<n;++i)
const int N=600005;
int n,m,cnt,L,a[N],b[N],c[N],d[N],p[N],q[N],o[N],rk[N]; LL sum,ans;
bool cmp(const int i,const int j){return a[i]==a[j]?rk[i]<rk[j]:a[i]<a[j];}
int main()
{
	scanf("%d%d",&n,&m);
	rep(i,n){
		scanf("%d%d",a+i,b+i),a[i]*=2,b[i]*=2,c[i]=a[i],a[n+i]=b[i]-a[i];
		if (b[i]/2<a[i]) a[i]=a[n+i]=b[i]/2; rk[i]=i*2,rk[i+n]=i*2+1;
	}
	rep(i,2*n) p[i]=i; sort(p,p+2*n,cmp); rep(i,m) d[p[i]]=1;
	//rep(i,m) printf("%d ",p[i]); puts("");
	rep(i,m) ans+=a[p[i]],++o[p[i]<n?p[i]:p[i]-n];
	int x=p[m-1],y=x;
	if (x<n && !d[x+n] && c[x]!=a[x]){
		sum=ans-a[x],ans=sum+c[x],y=x,--o[x];
		rep(i,n) if (i!=x && d[i] && !d[i+n] && a[i]==c[i]){
			if (sum+a[i+n]<ans) ans=sum+a[i+n],y=i;
			if (sum-a[i]+b[x]<ans) ans=sum-a[i]+b[x],y=-i-1;
		}
		rep(i,n) if (i!=x && d[i] && d[i+n])
			if (sum-b[i]+c[i]+b[x]<ans) ans=sum-b[i]+c[i]+b[x],y=-i-1;
		rep(i,n) if (!d[i]) if (sum+c[i]<ans) ans=sum+c[i],y=i;
		if (y<0) y=-1-y,--o[y],o[x]+=2; else ++o[y<n?y:y-n];
	}
	printf("%I64d\n",ans/2);
	rep(i,n) printf("%d",o[i]);
	return 0;
}


//Coder: Balajiganapathi
#ifndef ONLINE_JUDGE
#   define DEBUG
#   define TRACE
#else
//#   define NDEBUG
#endif

#include <algorithm>
#include <bitset>
#include <deque>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<string> vs;
typedef vector<pi> vpi;

// Basic macros
#define fi          first
#define se          second
#define all(x)      (x).begin(), (x).end()
#define ini(a, v)   memset(a, v, sizeof(a))
#define re(i,s,n)  	for(int i=s;i<(n);++i)
#define rep(i,s,n)  for(int i=s;i<=(n);++i)
#define fo(i,n)     re(i,0,n)
#define rev(i,n,s)  for(int i=(n)-1;i>=s;--i)
#define repv(i,n,s) for(int i=(n);i>=s;--i)
#define fov(i,n)    rev(i,n,0)
#define pu          push_back
#define mp          make_pair
#define si(x)       (int)(x.size())

#ifdef TRACE
    #define trace(x)            cerr<<x<<endl;
    #define trace1(x)           cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
    #define trace2(x,y)         cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
    #define trace3(x,y,z)       cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
    #define trace4(a,b,c,d)     cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<endl;
    #define trace5(a,b,c,d,e)   cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<" | "#e" = "<<e<<endl;
    #define trace6(a,b,c,d,e,f) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<" | "#e" = "<<e<<" | "#f" = "<<f<<endl;
#else
    #define trace(x)
    #define trace1(x)
    #define trace2(x,y)
    #define trace3(x,y,z)
    #define trace4(a,b,c,d)
    #define trace5(a,b,c,d,e)
    #define trace6(a,b,c,d,e,f)
#endif

const int oo = 1000000009;
const double eps = 1e-6;
const int mod = 1000000007;
const int mx = 300005;

int f[mx], s[mx], n, w;
int play[mx];

int main() {
    scanf("%d%d", &n, &w);
    fo(i, n) scanf("%d %d", f + i, s + i);
    set<pi> s1, s2;
    fo(i, n) {
        s1.insert(mp(f[i], i));
        s2.insert(mp(s[i], i));
    }

    ll ans = 0;
    while(w > 0) {
        if(w == 1) {
            pi cur = *s1.begin();
            ans += cur.fi;
            ++play[cur.se];
            w = 0;
            break;
        }
        int from = 0;
        if(si(s1) >= 2 && si(s2) > 0) {
            pi cur1 = *s1.begin();
            s1.erase(s1.begin());
            pi cur2 = *s1.begin();
            s1.insert(cur1);
            pi cur = *s2.begin();
            if(cur.fi <= cur1.fi + cur2.fi) {
                from = 1;
            } else {
                from = 0;
            }
        } else if(si(s2) == 0 && si(s1) >= 2) {
            from = 0;
        } else {
            from = 1;
        }

        if(from == 0) {
            assert(si(s1) >= 1);
            pi cur1 = *s1.begin();
            s1.erase(s1.begin());
            //pi cur2 = *s1.begin();
            //s1.erase(s1.begin());
            ans += cur1.fi;
            //ans += cur2.fi;
            int i = cur1.se;
            if(++play[i] == 1) {
                s2.erase(s2.find(mp(s[i], i)));
                s1.insert(mp(s[i] - f[i], i));
            }
            //i = cur2.se;
            //if(++play[i] == 1) {
                //s2.erase(s2.find(mp(s[i], i)));
                //s1.insert(mp(s[i] - f[i], i));
            //}
        } else {
            assert(si(s2) >= 1);
            pi cur = *s2.begin();
            ans += cur.fi;
            int i = cur.se;
            play[i] = 2;
            s2.erase(s2.begin());
            s1.erase(s1.find(mp(f[i], i)));
        }
        w -= 2;
    }

    printf("%lld\n", ans);
    fo(i, n) printf("%d", play[i]);
    printf("\n");

	return 0;
}


//Coder: Balajiganapathi
#ifndef ONLINE_JUDGE
#   define DEBUG
#   define TRACE
#else
//#   define NDEBUG
#endif

#include <algorithm>
#include <bitset>
#include <deque>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<string> vs;
typedef vector<pi> vpi;

// Basic macros
#define fi          first
#define se          second
#define all(x)      (x).begin(), (x).end()
#define ini(a, v)   memset(a, v, sizeof(a))
#define re(i,s,n)  	for(int i=s;i<(n);++i)
#define rep(i,s,n)  for(int i=s;i<=(n);++i)
#define fo(i,n)     re(i,0,n)
#define rev(i,n,s)  for(int i=(n)-1;i>=s;--i)
#define repv(i,n,s) for(int i=(n);i>=s;--i)
#define fov(i,n)    rev(i,n,0)
#define pu          push_back
#define mp          make_pair
#define si(x)       (int)(x.size())

#ifdef TRACE
    #define trace(x)            cerr<<x<<endl;
    #define trace1(x)           cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
    #define trace2(x,y)         cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
    #define trace3(x,y,z)       cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
    #define trace4(a,b,c,d)     cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<endl;
    #define trace5(a,b,c,d,e)   cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<" | "#e" = "<<e<<endl;
    #define trace6(a,b,c,d,e,f) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<" | "#e" = "<<e<<" | "#f" = "<<f<<endl;
#else
    #define trace(x)
    #define trace1(x)
    #define trace2(x,y)
    #define trace3(x,y,z)
    #define trace4(a,b,c,d)
    #define trace5(a,b,c,d,e)
    #define trace6(a,b,c,d,e,f)
#endif

const int oo = 1000000009;
const double eps = 1e-6;
const int mod = 1000000007;
const int mx = 300005;

int f[mx], s[mx], n, w;
int play[mx];

int main() {
    scanf("%d%d", &n, &w);
    fo(i, n) scanf("%d %d", f + i, s + i);
    set<pi> s1, s2;
    fo(i, n) {
        s1.insert(mp(f[i], i));
        s2.insert(mp(s[i], i));
    }

    ll ans = 0;
    while(w > 0) {
        if(w == 1) {
            pi cur = *s1.begin();
            ans += cur.fi;
            play[cur.se] = 1;
            w = 0;
            break;
        }
        int from = 0;
        if(si(s1) >= 2 && si(s2) > 0) {
            pi cur1 = *s1.begin();
            pi cur2 = *s1.begin();
            pi cur = *s2.begin();
            if(cur.fi <= cur1.fi + cur2.fi) {
                from = 1;
            } else {
                from = 0;
            }
        } else if(si(s2) == 0) {
            from = 0;
        } else {
            from = 1;
        }

        if(from == 0) {
            pi cur1 = *s1.begin();
            pi cur2 = *s1.begin();
            s1.erase(s1.begin());
            s1.erase(s1.begin());
            ans += cur1.fi;
            ans += cur2.fi;
            int i = cur1.se;
            if(++play[i] == 1) {
                s2.erase(s2.find(mp(s[i], i)));
                s1.insert(mp(s[i] - f[i], i));
            }
            i = cur2.se;
            if(++play[i] == 1) {
                s2.erase(s2.find(mp(s[i], i)));
                s1.insert(mp(s[i] - f[i], i));
            }
        } else {
            pi cur = *s2.begin();
            ans += cur.fi;
            int i = cur.se;
            play[i] = 2;
            s2.erase(s2.begin());
            s1.erase(s1.find(mp(f[i], i)));
        }
        w -= 2;
    }

    printf("%lld\n", ans);
    fo(i, n) printf("%d", play[i]);
    printf("\n");

	return 0;
}


//Coder: Balajiganapathi
#define TRACE
#define DEBUG

#include <algorithm>
#include <bitset>
#include <deque>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<string> vs;

// Basic macros
#define st          first
#define se          second
#define all(x)      (x).begin(), (x).end()
#define ini(a, v)   memset(a, v, sizeof(a))
#define re(i,s,n)  	for(int i=s;i<(n);++i)
#define rep(i,s,n)  for(int i=s;i<=(n);++i)
#define fr(i,n)     re(i,0,n)
#define repv(i,f,t) for(int i = f; i >= t; --i)
#define rev(i,f,t)  repv(i,f - 1,t)
#define frv(i,n)    rev(i,n,0)
#define pu          push_back
#define mp          make_pair
#define sz(x)       (int)(x.size())

const int oo = 2000000009;
const double eps = 1e-9;

#ifdef TRACE
    #define trace1(x)                cerr << #x << ": " << x << endl;
    #define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
    #define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
    #define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
    #define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
    #define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

    #define trace1(x)
    #define trace2(x, y)
    #define trace3(x, y, z)
    #define trace4(a, b, c, d)
    #define trace5(a, b, c, d, e)
    #define trace6(a, b, c, d, e, f)

#endif

const int mx = 300005;
int n, t[mx][2], w;
int in1[mx], in2[mx];
ll best;

void doprint(int n1, int n2) {
    priority_queue<pi> pq1, pq2, pq01, pq02;
    assert(n1 + 2 * n2 == w);
    cout << best << endl;
    fr(i, n) {
        in1[i] = in2[i] = 0;
        pq01.push(mp(-t[i][0], i));
        pq02.push(mp(-t[i][1], i));
    }
    while(sz(pq1) < n1 || sz(pq2) < n2) {
        if(sz(pq1) < n1) {
            assert(!pq01.empty());
            pi cur = pq01.top(); pq01.pop();
            if(in1[cur.se] || in2[cur.se]) continue;
            in1[cur.se] = 1;
            pq1.push(mp(-t[cur.se][1], cur.se));
            continue;
        }
        if(sz(pq2) < n2) {
            while(!pq02.empty() && (in1[pq02.top().se] || in2[pq02.top().se])) pq02.pop();
            assert(!pq02.empty());
            pi cur = pq02.top();
            pq02.pop();
            if(n1) {
                pi cur1 = pq1.top();
                if(-cur1.st < -cur.st) {
                    pq02.push(cur);
                    cur = cur1;
                    pq1.pop();
                    in1[cur.se] = 0;
                }
            }
            in2[cur.se] = 1;
            pq2.push(mp(t[cur.se][1], cur.se));
        }
    }

    fr(i, n) {
        if(!in1[i] && !in2[i]) printf("0");
        else if(in1[i]) printf("1");
        else printf("2");
    }
    printf("\n");
}

int main() {
    scanf("%d %d", &n, &w);
    fr(i, n) scanf("%d %d", &t[i][0], &t[i][1]);

    priority_queue<pi> pq1, pq2, pq01, pq02;
    best = -1;
    int an1, an2;
    ll cost = 0;
    fr(i, n) {
        pq01.push(mp(-t[i][0], i));
        pq02.push(mp(-t[i][1], i));
    }
    for(int n1 = w % 2; n1 <= w; n1 += 2) {
        int n2 = (w - n1) / 2;
        assert(n1 + 2 * n2 == w);
        if(n1 + n2 > n) break;
        if(sz(pq2) > n2) {
            pi cur = pq2.top();
            in2[cur.se] = 0;
            pq01.push(mp(-t[cur.se][0], cur.se));
            pq02.push(mp(-t[cur.se][1], cur.se));
        }
        while(sz(pq1) < n1 || sz(pq2) < n2) {
            if(sz(pq1) < n1) {
                assert(!pq01.empty());
                pi cur = pq01.top(); pq01.pop();
                if(in1[cur.se] || in2[cur.se]) continue;
                in1[cur.se] = 1;
                pq1.push(mp(-t[cur.se][1], cur.se));
                cost += t[cur.se][0];
                continue;
            }
            if(sz(pq2) < n2) {
                while(!pq02.empty() && (in1[pq02.top().se] || in2[pq02.top().se])) pq02.pop();
                assert(!pq02.empty());
                pi cur = pq02.top();
                pq02.pop();
                if(n1) {
                    pi cur1 = pq1.top();
                    if(-cur1.st < -cur.st) {
                        pq02.push(cur);
                        cur = cur1;
                        pq1.pop();
                        in1[cur.se] = 0;
                        cost -= t[cur.se][0];
                    }
                }
                in2[cur.se] = 1;
                cost += t[cur.se][1];
                pq2.push(mp(t[cur.se][1], cur.se));
            }
        }
        if(best == -1 || cost < best) {
            best = cost;
            an1 = n1; an2 = n2;
        }
    }

    doprint(an1, an2);

	return 0;
}


//Coder: Balajiganapathi
#ifndef ONLINE_JUDGE
#   define DEBUG
#   define TRACE
#else
//#   define NDEBUG
#endif

#include <algorithm>
#include <bitset>
#include <deque>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<string> vs;
typedef vector<pi> vpi;

// Basic macros
#define fi          first
#define se          second
#define all(x)      (x).begin(), (x).end()
#define ini(a, v)   memset(a, v, sizeof(a))
#define re(i,s,n)  	for(int i=s;i<(n);++i)
#define rep(i,s,n)  for(int i=s;i<=(n);++i)
#define fo(i,n)     re(i,0,n)
#define rev(i,n,s)  for(int i=(n)-1;i>=s;--i)
#define repv(i,n,s) for(int i=(n);i>=s;--i)
#define fov(i,n)    rev(i,n,0)
#define pu          push_back
#define mp          make_pair
#define si(x)       (int)(x.size())

#ifdef TRACE
    #define trace(x)            cerr<<x<<endl;
    #define trace1(x)           cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
    #define trace2(x,y)         cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
    #define trace3(x,y,z)       cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
    #define trace4(a,b,c,d)     cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<endl;
    #define trace5(a,b,c,d,e)   cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<" | "#e" = "<<e<<endl;
    #define trace6(a,b,c,d,e,f) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<" | "#e" = "<<e<<" | "#f" = "<<f<<endl;
#else
    #define trace(x)
    #define trace1(x)
    #define trace2(x,y)
    #define trace3(x,y,z)
    #define trace4(a,b,c,d)
    #define trace5(a,b,c,d,e)
    #define trace6(a,b,c,d,e,f)
#endif

const int oo = 1000000009;
const double eps = 1e-6;
const int mod = 1000000007;
const int mx = 300005;

int f[mx], s[mx], n, w;
int play[mx];

int main() {
    scanf("%d%d", &n, &w);
    fo(i, n) scanf("%d %d", f + i, s + i);
    set<pi> s1, s2;
    fo(i, n) {
        s1.insert(mp(f[i], i));
        s2.insert(mp(s[i], i));
    }

    ll ans = 0;
    while(w > 0) {
        if(w == 1) {
            pi cur = *s1.begin();
            ans += cur.fi;
            w = 0;
            break;
        }
        int from = 0;
        if(si(s1) >= 2 && si(s2) > 0) {
            pi cur1 = *s1.begin();
            pi cur2 = *s1.begin();
            pi cur = *s2.begin();
            if(cur.fi <= cur1.fi + cur2.fi) {
                from = 1;
            } else {
                from = 0;
            }
        } else if(si(s2) == 0) {
            from = 0;
        } else {
            from = 1;
        }

        if(from == 0) {
            pi cur1 = *s1.begin();
            s1.erase(s1.begin());
            pi cur2 = *s1.begin();
            s1.erase(s1.begin());
            ans += cur1.fi;
            ans += cur2.fi;
            int i = cur1.se;
            if(++play[i] == 1) {
                s2.erase(s2.find(mp(s[i], i)));
                s1.insert(mp(s[i] - f[i], i));
            }
            i = cur2.se;
            if(++play[i] == 1) {
                s2.erase(s2.find(mp(s[i], i)));
                s1.insert(mp(s[i] - f[i], i));
            }
        } else {
            pi cur = *s2.begin();
            ans += cur.fi;
            int i = cur.se;
            play[i] = 2;
            s2.erase(s2.begin());
            s1.erase(s1.find(mp(f[i], i)));
        }
        w -= 2;
    }

    printf("%lld\n", ans);
    fo(i, n) printf("%d", play[i]);
    printf("\n");

	return 0;
}


//Coder: Balajiganapathi
#ifndef ONLINE_JUDGE
#   define DEBUG
#   define TRACE
#else
//#   define NDEBUG
#endif

#include <algorithm>
#include <bitset>
#include <deque>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<string> vs;
typedef vector<pi> vpi;

// Basic macros
#define fi          first
#define se          second
#define all(x)      (x).begin(), (x).end()
#define ini(a, v)   memset(a, v, sizeof(a))
#define re(i,s,n)  	for(int i=s;i<(n);++i)
#define rep(i,s,n)  for(int i=s;i<=(n);++i)
#define fo(i,n)     re(i,0,n)
#define rev(i,n,s)  for(int i=(n)-1;i>=s;--i)
#define repv(i,n,s) for(int i=(n);i>=s;--i)
#define fov(i,n)    rev(i,n,0)
#define pu          push_back
#define mp          make_pair
#define si(x)       (int)(x.size())

#ifdef TRACE
    #define trace(x)            cerr<<x<<endl;
    #define trace1(x)           cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
    #define trace2(x,y)         cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
    #define trace3(x,y,z)       cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
    #define trace4(a,b,c,d)     cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<endl;
    #define trace5(a,b,c,d,e)   cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<" | "#e" = "<<e<<endl;
    #define trace6(a,b,c,d,e,f) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<" | "#e" = "<<e<<" | "#f" = "<<f<<endl;
#else
    #define trace(x)
    #define trace1(x)
    #define trace2(x,y)
    #define trace3(x,y,z)
    #define trace4(a,b,c,d)
    #define trace5(a,b,c,d,e)
    #define trace6(a,b,c,d,e,f)
#endif

const int oo = 1000000009;
const double eps = 1e-6;
const int mod = 1000000007;
const int mx = 300005;

int f[mx], s[mx], n, w;
int play[mx];

int main() {
    scanf("%d%d", &n, &w);
    fo(i, n) scanf("%d %d", f + i, s + i);
    set<pi> s1, s2;
    fo(i, n) {
        s1.insert(mp(f[i], i));
        s2.insert(mp(s[i], i));
    }

    ll ans = 0;
    while(w > 0) {
        if(w == 1) {
            assert(si(s1) >= 1);
            pi cur = *s1.begin();
            ans += cur.fi;
            ++play[cur.se];
            w = 0;
            break;
        }
        int from = 0;
        if(si(s1) >= 2 && si(s2) > 0) {
            pi cur1 = *s1.begin();
            s1.erase(s1.begin());
            pi cur2 = *s1.begin();
            s1.insert(cur1);
            pi cur = *s2.begin();
            if(cur.fi <= cur1.fi + cur2.fi) {
                from = 1;
            } else {
                from = 0;
            }
        } else if(si(s2) == 0 && si(s1) >= 2) {
            from = 0;
        } else {
            from = 1;
        }

        if(from == 0) {
            assert(si(s1) >= 2);
            pi cur1 = *s1.begin();
            s1.erase(s1.begin());
            pi cur2 = *s1.begin();
            s1.erase(s1.begin());
            ans += cur1.fi;
            ans += cur2.fi;
            int i = cur1.se;
            if(++play[i] == 1) {
                s2.erase(s2.find(mp(s[i], i)));
                s1.insert(mp(s[i] - f[i], i));
            }
            i = cur2.se;
            if(++play[i] == 1) {
                s2.erase(s2.find(mp(s[i], i)));
                s1.insert(mp(s[i] - f[i], i));
            }
        } else {
            assert(si(s2) >= 1);
            pi cur = *s2.begin();
            ans += cur.fi;
            int i = cur.se;
            play[i] = 2;
            s2.erase(s2.begin());
            s1.erase(s1.find(mp(f[i], i)));
        }
        w -= 2;
    }

    cout << ans << endl;
    fo(i, n) printf("%d", play[i]);
    printf("\n");

	return 0;
}


//Coder: Balajiganapathi
#ifndef ONLINE_JUDGE
#   define DEBUG
#   define TRACE
#else
//#   define NDEBUG
#endif

#include <algorithm>
#include <bitset>
#include <deque>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<string> vs;
typedef vector<pi> vpi;

// Basic macros
#define fi          first
#define se          second
#define all(x)      (x).begin(), (x).end()
#define ini(a, v)   memset(a, v, sizeof(a))
#define re(i,s,n)  	for(int i=s;i<(n);++i)
#define rep(i,s,n)  for(int i=s;i<=(n);++i)
#define fo(i,n)     re(i,0,n)
#define rev(i,n,s)  for(int i=(n)-1;i>=s;--i)
#define repv(i,n,s) for(int i=(n);i>=s;--i)
#define fov(i,n)    rev(i,n,0)
#define pu          push_back
#define mp          make_pair
#define si(x)       (int)(x.size())

#ifdef TRACE
    #define trace(x)            cerr<<x<<endl;
    #define trace1(x)           cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
    #define trace2(x,y)         cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
    #define trace3(x,y,z)       cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
    #define trace4(a,b,c,d)     cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<endl;
    #define trace5(a,b,c,d,e)   cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<" | "#e" = "<<e<<endl;
    #define trace6(a,b,c,d,e,f) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<" | "#e" = "<<e<<" | "#f" = "<<f<<endl;
#else
    #define trace(x)
    #define trace1(x)
    #define trace2(x,y)
    #define trace3(x,y,z)
    #define trace4(a,b,c,d)
    #define trace5(a,b,c,d,e)
    #define trace6(a,b,c,d,e,f)
#endif

const int oo = 1000000009;
const double eps = 1e-6;
const int mod = 1000000007;
const int mx = 300005;

int f[mx], s[mx], n, w;
int play[mx];

int main() {
    scanf("%d%d", &n, &w);
    fo(i, n) scanf("%d %d", f + i, s + i);
    set<pair<int, pair<int, int> > > s1, s2;
    fo(i, n) {
        s1.insert(mp(f[i], mp(s[i] - f[i], i)));
        s2.insert(mp(s[i], mp(-1 * f[i], i)));
    }

    ll ans = 0;
    while(w > 0) {
        if((w & 1) || si(s1) == 1) {
            assert(si(s1) >= 1);
            pair<int, pi> cur = *s1.begin();
            ans += cur.fi;
            if(++play[cur.se.se] == 1) {
                int i = cur.se.se;
                s2.erase(mp(s[i], mp(-1 * f[i], i)));
                s1.insert(mp(s[i] - f[i], mp(oo, i)));
            }
            --w;
            s1.erase(s1.begin());
            continue;
        }
        int from = 0;
        if(si(s1) >= 2 && si(s2) > 0) {
            pair<int, pi> cur1 = *s1.begin();
            s1.erase(s1.begin());
            pair<int, pi> cur2 = *s1.begin();
            s1.insert(cur1);
            pair<int, pi> cur = *s2.begin();
            if(cur.fi <= cur1.fi + cur2.fi) {
                from = 1;
            } else {
                from = 0;
            }
            trace4(from, cur1.fi, cur2.fi, cur.fi);
        } else if(si(s2) == 0 && si(s1) >= 2) {
            from = 0;
        } else {
            from = 1;
        }

        if(from == 0) {
            assert(si(s1) >= 2);
            pair<int, pi> cur1 = *s1.begin();
            s1.erase(s1.begin());
            pair<int, pi> cur2 = *s1.begin();
            s1.erase(s1.begin());
            ans += cur1.fi;
            ans += cur2.fi;
            int i = cur1.se.se;
            if(++play[i] == 1) {
                s2.erase(s2.find(mp(s[i], mp(-1 * f[i], i))));
                s1.insert(mp(s[i] - f[i], mp(oo, i)));
            }
            i = cur2.se.se;
            if(++play[i] == 1) {
                s2.erase(s2.find(mp(s[i], mp(-1 * f[i], i))));
                s1.insert(mp(s[i] - f[i], mp(oo, i)));
            }
        } else {
            assert(si(s2) >= 1);
            pair<int, pi> cur = *s2.begin();
            ans += cur.fi;
            int i = cur.se.se;
            play[i] = 2;
            s2.erase(s2.begin());
            s1.erase(s1.find(mp(f[i], mp(s[i] - f[i], i))));
        }
        w -= 2;
    }

    cout << ans << endl;
    fo(i, n) printf("%d", play[i]);
    printf("\n");

	return 0;
}


//Coder: Balajiganapathi
#ifndef ONLINE_JUDGE
#   define DEBUG
#   define TRACE
#else
//#   define NDEBUG
#endif

#include <algorithm>
#include <bitset>
#include <deque>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<string> vs;
typedef vector<pi> vpi;

// Basic macros
#define fi          first
#define se          second
#define all(x)      (x).begin(), (x).end()
#define ini(a, v)   memset(a, v, sizeof(a))
#define re(i,s,n)  	for(int i=s;i<(n);++i)
#define rep(i,s,n)  for(int i=s;i<=(n);++i)
#define fo(i,n)     re(i,0,n)
#define rev(i,n,s)  for(int i=(n)-1;i>=s;--i)
#define repv(i,n,s) for(int i=(n);i>=s;--i)
#define fov(i,n)    rev(i,n,0)
#define pu          push_back
#define mp          make_pair
#define si(x)       (int)(x.size())

#ifdef TRACE
    #define trace(x)            cerr<<x<<endl;
    #define trace1(x)           cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
    #define trace2(x,y)         cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
    #define trace3(x,y,z)       cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
    #define trace4(a,b,c,d)     cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<endl;
    #define trace5(a,b,c,d,e)   cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<" | "#e" = "<<e<<endl;
    #define trace6(a,b,c,d,e,f) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<" | "#e" = "<<e<<" | "#f" = "<<f<<endl;
#else
    #define trace(x)
    #define trace1(x)
    #define trace2(x,y)
    #define trace3(x,y,z)
    #define trace4(a,b,c,d)
    #define trace5(a,b,c,d,e)
    #define trace6(a,b,c,d,e,f)
#endif

const int oo = 1000000009;
const double eps = 1e-6;
const int mod = 1000000007;
const int mx = 300005;

int f[mx], s[mx], n, w;
int play[mx];

int main() {
    scanf("%d%d", &n, &w);
    fo(i, n) scanf("%d %d", f + i, s + i);
    set<pair<int, pair<int, int> > > s1, s2;
    fo(i, n) {
        s1.insert(mp(f[i], mp(s[i] - f[i], i)));
        s2.insert(mp(s[i], mp(-1 * f[i], i)));
    }

    ll ans = 0;
    while(w > 0) {
        if(w == 1) {
            assert(si(s1) >= 1);
            pair<int, pi> cur = *s1.begin();
            ans += cur.fi;
            ++play[cur.se.se];
            w = 0;
            break;
        }
        int from = 0;
        if(si(s1) >= 2 && si(s2) > 0) {
            pair<int, pi> cur1 = *s1.begin();
            s1.erase(s1.begin());
            pair<int, pi> cur2 = *s1.begin();
            s1.insert(cur1);
            pair<int, pi> cur = *s2.begin();
            if(cur.fi <= cur1.fi + cur2.fi) {
                from = 1;
            } else {
                from = 0;
            }
        } else if(si(s2) == 0 && si(s1) >= 2) {
            from = 0;
        } else {
            from = 1;
        }

        if(from == 0) {
            assert(si(s1) >= 2);
            pair<int, pi> cur1 = *s1.begin();
            s1.erase(s1.begin());
            pair<int, pi> cur2 = *s1.begin();
            s1.erase(s1.begin());
            ans += cur1.fi;
            ans += cur2.fi;
            int i = cur1.se.se;
            if(++play[i] == 1) {
                s2.erase(s2.find(mp(s[i], mp(-1 * f[i], i))));
                s1.insert(mp(s[i] - f[i], mp(oo, i)));
            }
            i = cur2.se.se;
            if(++play[i] == 1) {
                s2.erase(s2.find(mp(s[i], mp(-1 * f[i], i))));
                s1.insert(mp(s[i] - f[i], mp(oo, i)));
            }
        } else {
            assert(si(s2) >= 1);
            pair<int, pi> cur = *s2.begin();
            ans += cur.fi;
            int i = cur.se.se;
            play[i] = 2;
            s2.erase(s2.begin());
            s1.erase(s1.find(mp(f[i], mp(s[i] - f[i], i))));
        }
        w -= 2;
    }

    cout << ans << endl;
    fo(i, n) printf("%d", play[i]);
    printf("\n");

	return 0;
}