#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return a % b ? gcd(b, a % b) : b; }
struct Obj {
  int val, ind;
};
bool operator<(const Obj &a, const Obj &b) { return a.val > b.val; }
int cmp(const Obj &a, const Obj &b) { return a.val < b.val; }
Obj s[300001];
int se[300001];
int use[300001];
priority_queue<Obj> que;
int main() {
  int n, w;
  int i, j, k;
  scanf("%d%d", &n, &w);
  for (i = 0; i < n; i++) {
    Obj temp;
    scanf("%d%d", &temp.val, &s[i].val);
    temp.ind = s[i].ind = i;
    se[i] = s[i].val;
    que.push(temp);
  }
  int sInd(0);
  long long ans(0);
  sort(s, s + n, cmp);
  for (i = 0; i < w; i++) {
    Obj a = que.top();
    while (use[a.ind] == 2) {
      que.pop();
      a = que.top();
    }
    que.pop();
    Obj b;
    if (!que.empty()) {
      b = que.top();
      while (use[b.ind] == 2) {
        que.pop();
        if (que.empty()) break;
        b = que.top();
      }
    }
    while (sInd < n && use[s[sInd].ind]) sInd++;
    if (i < w - 1 && sInd < n && s[sInd].val < a.val + b.val &&
        (use[a.ind] || se[a.ind] > s[sInd].val)) {
      ans += s[sInd].val;
      que.push(a);
      use[s[sInd].ind] = 2;
      i++;
    } else {
      ans += a.val;
      if (!use[a.ind]) {
        a.val = se[a.ind] - a.val;
        que.push(a);
      }
      use[a.ind]++;
    }
  }
  cout << ans << endl;
  for (i = 0; i < n; i++) {
    cout << use[i];
  }
  cout << endl;
}
