#include <bits/stdc++.h>
#define F(i, a, b) for(int i = (a); i <= (b); ++i)
#define dF(i, a, b) for(int i = (a); i >= (b); --i)
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
const int N = 3e5 + 5;
const int inf = 1e9;
int n, w, a[N], b[N];
struct node { int u, val; } Max = {0, inf};
bool operator < (node x, node y) { return x.val > y.val; }
priority_queue<node> p1, p2, p3, p4, p5;
LL ans;
int c[N];
int main() {
freopen("dxw.in", "r", stdin);
freopen("dxw.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> w;
F(i, 1, n) cin >> a[i] >> b[i];
F(i, 1, n) p1.push({i, a[i]}), p2.push({i, b[i]});
F(i, 1, w) {
while (!p1.empty() && c[p1.top().u] != 0) p1.pop();
while (!p2.empty() && c[p2.top().u] != 0) p2.pop();
while (!p3.empty() && c[p3.top().u] != 1) p3.pop();
while (!p4.empty() && c[p4.top().u] != 1) p4.pop();
while (!p5.empty() && c[p5.top().u] != 2) p5.pop();
node u1 = p1.empty() ? Max : p1.top();
node u2 = p2.empty() ? Max : p2.top();
node u3 = p3.empty() ? Max : p3.top();
node u4 = p4.empty() ? Max : p5.top();
node u5 = p5.empty() ? Max : p5.top();
int s1 = u1.val, s2 = u3.val;
int s3 = u2.val + u4.val;
int s4 = u2.val + u5.val;
int mn = min(min(s1, s2), min(s3, s4));
ans += mn;
if (mn == s1)
p1.pop(),
c[u1.u] = 1,
p3.push({u1.u, b[u1.u] - a[u1.u]}),
p4.push({u1.u, -a[u1.u]});
else if (mn == s2)
p3.pop(),
c[u3.u] = 2,
p5.push({u3.u, a[u3.u] - b[u3.u]});
else if (mn == s3)
p2.pop(), p4.pop(),
c[u2.u] = 2, c[u4.u] = 0,
p5.push({u2.u, a[u2.u] - b[u2.u]}),
p1.push({u4.u, a[u4.u]}),
p2.push({u4.u, b[u4.u]});
else
p2.pop(), p5.pop(),
c[u2.u] = 2, c[u5.u] = 1,
p5.push({u2.u, a[u2.u] - b[u2.u]}),
p3.push({u5.u, b[u5.u] - a[u5.u]}),
p4.push({u5.u, -a[u5.u]});
}
cout << ans << "\n";
F(i, 1, n) cout << c[i];
return 0;
}