#include <bits/stdc++.h>
using namespace std;
int n, ans[300001], w, times[300001];
pair<int, int> cost[300001];
long long answ;
struct one {
  int id, cost;
  bool operator()(one a, one b) {
    if (a.cost < b.cost) return true;
    if (b.cost < a.cost) return false;
    return (a.id < b.id);
  }
};
set<one, one> my_set_one;
struct two {
  int id, cost;
  bool operator()(two a, two b) {
    if (a.cost < b.cost) return true;
    if (b.cost < a.cost) return false;
    return (a.id < b.id);
  }
};
set<two, two> my_set_two;
int main() {
  scanf("%d %d", &n, &w);
  for (int i = 0; i < n; i++) {
    int f, s;
    scanf("%d %d", &f, &s);
    cost[i].first = f;
    cost[i].second = s;
    one new_one;
    new_one.cost = f;
    new_one.id = i;
    my_set_one.insert(new_one);
    two new_two;
    new_two.cost = s;
    new_two.id = i;
    my_set_two.insert(new_two);
  }
  int cnt = 0;
  while (cnt < w) {
    if (cnt + 2 <= w) {
      set<one, one>::iterator it = (my_set_one.begin());
      one f_one_p = *it;
      ++it;
      one s_one_p;
      if (it == my_set_one.end()) {
        s_one_p.cost = 2e9 + 1;
        s_one_p.id = -1;
      } else
        s_one_p = *it;
      set<two, two>::iterator it_t = (my_set_two.begin());
      two two_p;
      if (it_t != my_set_two.end())
        two_p = *it_t;
      else {
        two_p.cost = -1;
        two_p.id = -1;
      }
      if (two_p.cost == -1 || (f_one_p.cost + s_one_p.cost < two_p.cost)) {
        answ += f_one_p.cost;
        cnt++;
        my_set_one.erase(f_one_p);
        two d;
        d.cost = cost[f_one_p.id].second;
        d.id = f_one_p.id;
        my_set_two.erase(d);
        if (++times[f_one_p.id] == 1) {
          one new_one_f;
          new_one_f.id = f_one_p.id;
          new_one_f.cost = cost[new_one_f.id].second - cost[new_one_f.id].first;
          my_set_one.insert(new_one_f);
        }
      } else {
        answ += cost[two_p.id].first;
        cnt++;
        times[two_p.id]++;
        one d;
        d.cost = cost[two_p.id].first;
        d.id = two_p.id;
        my_set_one.erase(d);
        my_set_two.erase(two_p);
        one new_one;
        new_one.cost = cost[two_p.id].second - cost[two_p.id].first;
        new_one.id = two_p.id;
        my_set_one.insert(new_one);
      }
    } else {
      one p_one;
      p_one = *my_set_one.begin();
      answ += p_one.cost;
      times[p_one.id]++;
      cnt++;
    }
  }
  printf("%I64d\n", answ);
  for (int i = 0; i < n; i++) {
    printf("%d", times[i]);
  }
  return 0;
}
