#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <set>
#include <utility>
#include <vector>
#include <queue>

using namespace std;

typedef pair<int,int> PII;
typedef pair<int,PII> PIII;

#define LL long long
#define ULL unsigned long long
#define m_p make_pair
#define l_b lower_bound
#define p_b push_back
#define w1 first
#define w2 second
#define maxlongint 2147483647
#define biglongint 2139062143

const int maxn=300005;

struct avl
{
    avl *lchild,*rchild,*father;
    int num,depth,size,biao;
    LL sum;
} *root,*null;

int a[maxn],b[maxn],getans[maxn],place[maxn];
LL sum[maxn];
PIII ls[maxn];
int N,W;
LL ans;

void update(avl *p)
{
    if (p==null) return;
    p->depth=max(p->lchild->depth,p->rchild->depth)+1;
    p->sum=p->lchild->sum+p->rchild->sum+p->num;
    p->size=p->lchild->size+p->rchild->size+1;
}

void spinl(avl *a)
{
    avl *b=a->lchild;
    a->lchild=b->rchild;
    b->rchild->father=a;
    b->rchild=a;
    b->father=a->father;
    a->father=b;
    update(a);
    update(b);
    if (b!=null)
    {
        if (b->father->lchild==a) b->father->lchild=b;
                            else  b->father->rchild=b;
    }
}

void spinr(avl *a)
{
    avl *b=a->rchild;
    a->rchild=b->lchild;
    b->lchild->father=a;
    b->lchild=a;
    b->father=a->father;
    a->father=b;
    update(a);
    update(b);
    if (b!=null)
    {
        if (b->father->lchild==a) b->father->lchild=b;
                            else  b->father->rchild=b;
    }
}

void insert(avl *&p,int num,int biao,avl *father)
{
    if (p==null)
    {
        avl *q=new avl;
        q->father=father;
        q->lchild=q->rchild=null;
        q->num=num;
        q->sum=num;
        q->size=1;
        q->biao=biao;
        p=q;
    }
    else
    {
        if ((num>p->num)||((num==p->num)&&(biao>p->biao))) insert(p->rchild,num,biao,p); else insert(p->lchild,num,biao,p);
        update(p);
        if (p->depth>2)
            if (abs(p->lchild->depth-p->rchild->depth)>=2)
            {
                if (p->lchild->depth>p->rchild->depth)
                    spinl(p);
                else
                    spinr(p);
            }
    }
}

void copyit(avl *p,avl *q)
{
    p->num=q->num,p->biao=q->biao,p->sum=q->sum;
}

void erasenode(avl *p)
{
    avl *gx;
    if ((p->lchild==null)&&(p->rchild==null))
    {
        if (p->father->lchild==p) p->father->lchild=null; else p->father->rchild=null;
        gx=p->father;
    }
    else
    {
        if (p->lchild==null)
        {
            if (p->father->lchild==p) p->father->lchild=p->rchild; else p->father->rchild=p->rchild;
            p->rchild->father=p->father;
            gx=p->father;
        }
        else
            if (p->rchild==null)
            {
                if (p->father->lchild==p) p->father->lchild=p->lchild; else p->father->rchild=p->lchild;
                p->lchild->father=p->father;
                gx=p->father;
            }
            else
            {
                avl *q=p->rchild;
                if (q->lchild==null)
                {
                    copyit(p,q);
                    p->rchild=q->rchild;
                    q->rchild->father=p;
                    gx=p;
                }
                else
                {
                    while (q->lchild!=null) q=q->lchild;
                    copyit(p,q);
                    q->father->lchild=q->rchild;
                    q->rchild->father=q->father;
                    gx=q->father;
                }
            }
    }
    while (gx!=null)
    {
        update(gx);
        gx=gx->father;
    }
}

void erase(avl *p,int num,int biao)
{
    if (biao==p->biao) erasenode(p); else
    {
        if ((num>p->num)||((num==p->num)&&(biao>p->biao)))
            erase(p->rchild,num,biao);
        else
            erase(p->lchild,num,biao);
    }
}

LL getkthsum(int k)
{
    LL sum=0;
    avl *p=root;
    while (p!=null)
    {
        if (k>=p->lchild->size+1)
        {
            sum+=p->lchild->sum+p->num;
            k-=p->lchild->size+1;
            p=p->rchild;
        }
        else
            p=p->lchild;
    }
    return sum;
}

void Gua(avl *p,int k)
{
    if (p==null) return;
    if (k>=p->lchild->size+1)
    {
        Gua(p->lchild,p->lchild->size);
        getans[place[p->biao]]++;
        Gua(p->rchild,k-p->lchild->size-1);
    }
    else
        Gua(p->lchild,k);
}

int main()
{
    null=new avl;
    null->lchild=null->rchild=null->father=null;
    null->num=null->depth=null->size=0;
    null->sum=0;

    //freopen("E.in","r",stdin);
    scanf("%d %d",&N,&W);
    for (int i=1;i<=N;i++) scanf("%d %d",&a[i],&b[i]);
    for (int i=1;i<=N;i++) ls[i]=m_p(b[i],m_p(a[i],i));
    sort(ls+1,ls+N+1);
    sum[0]=0;
    for (int i=1;i<=N;i++) a[i]=ls[i].w2.w1,b[i]=ls[i].w1,sum[i]=sum[i-1]+a[i],place[i]=ls[i].w2.w2;

    root=new avl;
    root->lchild=root->rchild=root->father=null;
    root->num=maxlongint;
    root->biao=0;
    root->depth=root->size=1;
    root->sum=0;

    for (int i=1;i<=N;i++) insert(root->lchild,a[i],i,root);
    ans=(LL)maxlongint*maxlongint;
    for (int i=1;i<=N+1;i++)
    {
        if ((i-1<=W)&&(i-1+N>=W)) ans=min(ans,sum[i-1]+getkthsum(W-i+1));
        if (i==N+1) break;
        erase(root->lchild,a[i],i);
        insert(root->lchild,b[i]-a[i],i,root);
    }

    printf("%I64d\n",ans);

    memset(getans,0,sizeof(getans));
    root=new avl;
    root->lchild=root->rchild=root->father=null;
    root->num=maxlongint;
    root->biao=0;
    root->depth=root->size=1;
    root->sum=0;
    for (int i=1;i<=N;i++) insert(root->lchild,a[i],i,root);
    for (int i=1;i<=N+1;i++)
    {
        if ((i-1<=W)&&(i-1+N>=W))
            if (ans==sum[i-1]+getkthsum(W-i+1))
            {
                for (int j=1;j<=i-1;j++) getans[place[j]]++;
                Gua(root->lchild,W-i+1);
                break;
            }
        erase(root->lchild,a[i],i);
        insert(root->lchild,b[i]-a[i],i,root);
    }
    for (int i=1;i<=N;i++) printf("%d",getans[i]);printf("\n");

    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <set>
#include <utility>
#include <vector>
#include <queue>

using namespace std;

typedef pair<int,int> PII;
typedef pair<int,PII> PIII;

#define LL long long
#define ULL unsigned long long
#define m_p make_pair
#define l_b lower_bound
#define p_b push_back
#define w1 first
#define w2 second
#define maxlongint 2147483647
#define biglongint 2139062143

const int maxn=300005;

struct avl
{
    avl *lchild,*rchild,*father;
    int num,depth,size,biao;
    LL sum;
} *root,*null;

int a[maxn],b[maxn],getans[maxn],place[maxn];
LL sum[maxn];
PIII ls[maxn];
int N,W;
LL ans;

void update(avl *p)
{
    if (p==null) return;
    p->depth=max(p->lchild->depth,p->rchild->depth)+1;
    p->sum=p->lchild->sum+p->rchild->sum+p->num;
    p->size=p->lchild->size+p->rchild->size+1;
}

void spinl(avl *a)
{
    avl *b=a->lchild;
    if (root==a) root=b;
    a->lchild=b->rchild;
    b->rchild->father=a;
    b->rchild=a;
    b->father=a->father;
    a->father=b;
    update(a);
    update(b);
    if ((b!=null)&&(b->father!=null))
    {
        if (b->father->lchild==a) b->father->lchild=b;
                            else  b->father->rchild=b;
    }
}

void spinr(avl *a)
{
    avl *b=a->rchild;
    if (root==a) root=b;
    a->rchild=b->lchild;
    b->lchild->father=a;
    b->lchild=a;
    b->father=a->father;
    a->father=b;
    update(a);
    update(b);
    if ((b!=null)&&(b->father!=null))
    {
        if (b->father->lchild==a) b->father->lchild=b;
                            else  b->father->rchild=b;
    }
}

void insert(avl *&p,int num,int biao,avl *father)
{
    if (p==null)
    {
        avl *q=new avl;
        q->father=father;
        q->lchild=q->rchild=null;
        q->num=num;
        q->sum=num;
        q->size=1;
        q->biao=biao;
        p=q;
    }
    else
    {
        if ((num>p->num)||((num==p->num)&&(biao>p->biao))) insert(p->rchild,num,biao,p); else insert(p->lchild,num,biao,p);
        update(p);
        if (p->depth>2)
            if (abs(p->lchild->depth-p->rchild->depth)>=2)
            {
                if (p->lchild->depth>p->rchild->depth)
                    spinl(p);
                else
                    spinr(p);
            }
    }
}

void copyit(avl *p,avl *q)
{
    p->num=q->num,p->biao=q->biao,p->sum=q->sum;
}

void erasenode(avl *p)
{
    avl *gx;
    if ((p->lchild==null)&&(p->rchild==null))
    {
        if (p->father->lchild==p) p->father->lchild=null; else p->father->rchild=null;
        gx=p->father;
    }
    else
    {
        if (p->lchild==null)
        {
            if (p->father->lchild==p) p->father->lchild=p->rchild; else p->father->rchild=p->rchild;
            p->rchild->father=p->father;
            gx=p->father;
        }
        else
            if (p->rchild==null)
            {
                if (p->father->lchild==p) p->father->lchild=p->lchild; else p->father->rchild=p->lchild;
                p->lchild->father=p->father;
                gx=p->father;
            }
            else
            {
                avl *q=p->rchild;
                if (q->lchild==null)
                {
                    copyit(p,q);
                    p->rchild=q->rchild;
                    q->rchild->father=p;
                    gx=p;
                }
                else
                {
                    while (q->lchild!=null) q=q->lchild;
                    copyit(p,q);
                    q->father->lchild=q->rchild;
                    q->rchild->father=q->father;
                    gx=q->father;
                }
            }
    }
    while (gx!=null)
    {
        update(gx);
        gx=gx->father;
    }
}

void erase(avl *p,int num,int biao)
{
    if (biao==p->biao) erasenode(p); else
    {
        if ((num>p->num)||((num==p->num)&&(biao>p->biao)))
            erase(p->rchild,num,biao);
        else
            erase(p->lchild,num,biao);
    }
}

LL getkthsum(int k)
{
    LL sum=0;
    avl *p=root;
    while (p!=null)
    {
        if (k>=p->lchild->size+1)
        {
            sum+=p->lchild->sum+p->num;
            k-=p->lchild->size+1;
            p=p->rchild;
        }
        else
            p=p->lchild;
    }
    return sum;
}

void Gua(avl *p,int k)
{
    if (p==null) return;
    if (k>=p->lchild->size+1)
    {
        Gua(p->lchild,p->lchild->size);
        getans[place[p->biao]]++;
        Gua(p->rchild,k-p->lchild->size-1);
    }
    else
        Gua(p->lchild,k);
}

int main()
{
    null=new avl;
    null->lchild=null->rchild=null->father=null;
    null->num=null->depth=null->size=0;
    null->sum=0;

    //freopen("E.in","r",stdin);
    scanf("%d %d",&N,&W);
    for (int i=1;i<=N;i++) scanf("%d %d",&a[i],&b[i]);
    for (int i=1;i<=N;i++) ls[i]=m_p(b[i],m_p(a[i],i));
    sort(ls+1,ls+N+1);
    sum[0]=0;
    for (int i=1;i<=N;i++) a[i]=ls[i].w2.w1,b[i]=ls[i].w1,sum[i]=sum[i-1]+a[i],place[i]=ls[i].w2.w2;

    root=null;
    for (int i=1;i<=N;i++) insert(root,a[i],i,null);
    ans=(LL)maxlongint*maxlongint;
    for (int i=1;i<=N+1;i++)
    {
        if ((i-1<=W)&&(i+N>=W)) ans=min(ans,sum[i-1]+getkthsum(W-i+1));
        if (i==N+1) break;
        erase(root,a[i],i);
        insert(root,b[i]-a[i],i,null);
    }

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

    memset(getans,0,sizeof(getans));
    root=null;
    for (int i=1;i<=N;i++) insert(root,a[i],i,null);
    for (int i=1;i<=N+1;i++)
    {
        if ((i-1<=W)&&(i+N>=W))
            if (ans==sum[i-1]+getkthsum(W-i+1))
            {
                for (int j=1;j<=i-1;j++) getans[place[j]]++;
                Gua(root,W-i+1);
                break;
            }
        erase(root,a[i],i);
        insert(root,b[i]-a[i],i,null);
    }
    for (int i=1;i<=N;i++) printf("%d",getans[i]);printf("\n");

    return 0;
}


#include <cstdio>
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

const int MAXN = 300100;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<pdi, pii> str;
int A[MAXN];
int B[MAXN];
int state[MAXN];

int N, W;
int get1(int x) {
  if (state[x] == 0) return A[x];
  else if (state[x] == 1) return B[x];
  else return -1;
}

priority_queue<str, vector<str>, greater<str> > pq;

int main() {
  scanf("%d %d", &N, &W);
  for(int i = 0; i < N; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    b -= a;
    A[i] = a;
    B[i] = b;
    pq.push(str(pdi(a, a), pii(i, 1)));
    pq.push(str(pdi((a + b) / 2., a + b), pii(i, 2)));
  }

  int took = 0;
  ll sum = 0;
  while (took < W) {
    str top = pq.top();
    int cur = top.second.first;
    int type = top.second.second;
    int cost = top.first.second;
//    printf("%d, %d\n", cur, type);
    pq.pop();
    if (type == 2 && state[cur] != 0) continue;
    if (type == 1 && get1(cur) != cost) continue;
    if (type == 2 && took + 1 == W) continue;

//    printf("taking %d %d for %d\n", cur, type, cost);
    took += type;
    state[cur] += type;
    sum += cost;
    if (state[cur] == 1) {
      pq.push(str(pdi(B[cur], B[cur]), pii(cur, 1)));
    }
  }

  int m2 = -1;
  for(int i = 0; i < N; ++i) {
    if (state[i] == 0) {
      if (m2 == -1 || A[i] + B[i] < A[m2] + B[m2]) {
        m2 = i;
      }
    }
  }

  int a = -1, b = -1;
  int va = -1, vb = -1;
  for(int i = 0; i < N; ++i) {
    if (state[i] == 0) continue;
    int val = A[i];
    if (state[i] == 2) val = B[i];
    if (a == -1 || val >= va) {
      b = a;
      a = i;
      vb = va;
      va = val;
    }
  }

  if (a > -1 && b > -1 && m2 > -1 && va + vb > A[m2] + B[m2]) {
    sum -= (va + vb);
    sum += A[m2] + B[m2];
    --state[a];
    --state[b];
    state[m2] += 2;
  }

  cout << sum << "\n";
  for(int i = 0; i < N; ++i) {
    printf("%d", state[i]);
  }
  printf("\n");
  return 0;
}


#include <cstdio>
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

const int MAXN = 300100;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<pdi, pii> str;
int A[MAXN];
int B[MAXN];
int state[MAXN];

int N, W;
int get1(int x) {
  if (state[x] == 0) return A[x];
  else if (state[x] == 1) return B[x];
  else return -1;
}

priority_queue<str, vector<str>, greater<str> > pq;

int main() {
  scanf("%d %d", &N, &W);
  for(int i = 0; i < N; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    b -= a;
    A[i] = a;
    B[i] = b;
    pq.push(str(pdi(a, a), pii(i, 1)));
    pq.push(str(pdi((a + b) / 2., a + b), pii(i, 2)));
  }

  int took = 0;
  ll sum = 0;
  while (took < W) {
    str top = pq.top();
    int cur = top.second.first;
    int type = top.second.second;
    int cost = top.first.second;
//    printf("%d, %d\n", cur, type);
    pq.pop();
    if (type == 2 && state[cur] != 0) continue;
    if (type == 1 && get1(cur) != cost) continue;
    if (type == 2 && took + 1 == W) continue;

//    printf("taking %d %d for %d\n", cur, type, cost);
    took += type;
    state[cur] += type;
    sum += cost;
    if (state[cur] == 1) {
      pq.push(str(pdi(B[cur], B[cur]), pii(cur, 1)));
    }
  }

  int m2 = -1;
  for(int i = 0; i < N; ++i) {
    if (state[i] == 0) {
      if (m2 == -1 || A[i] + B[i] < A[m2] + B[m2]) {
        m2 = i;
      }
    }
  }

  int a = -1, b = -1;
  int va = -1, vb = -1;
  for(int i = 0; i < N; ++i) {
    if (state[i] == 0) continue;
    int val = A[i];
    if (state[i] == 2) val = B[i];
    if (a == -1 || val >= va) {
      b = a;
      a = i;
      vb = va;
      va = val;
    }
  }
  if (va + vb > A[m2] + B[m2]) {
    sum -= (va + vb);
    sum += A[m2] + B[m2];
    --state[a];
    --state[b];
    state[m2] += 2;
  }

  cout << sum << "\n";
  for(int i = 0; i < N; ++i) {
    printf("%d", state[i]);
  }
  printf("\n");
  return 0;
}


#include <cstdio>
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

const int MAXN = 300100;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<pdi, pii> str;
int A[MAXN];
int B[MAXN];
int state[MAXN];

int N, W;
int get1(int x) {
  if (state[x] == 0) return A[x];
  else if (state[x] == 1) return B[x];
  else return -1;
}

priority_queue<str, vector<str>, greater<str> > pq;

int main() {
  scanf("%d %d", &N, &W);
  for(int i = 0; i < N; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    b -= a;
    A[i] = a;
    B[i] = b;
    pq.push(str(pdi(a, a), pii(i, 1)));
    pq.push(str(pdi((a + b) / 2., a + b), pii(i, 2)));
  }

  int took = 0;
  ll sum = 0;
  while (took < W) {
    str top = pq.top();
    int cur = top.second.first;
    int type = top.second.second;
    int cost = top.first.second;
//    printf("%d, %d\n", cur, type);
    pq.pop();
    if (type == 2 && state[cur] != 0) continue;
    if (type == 1 && get1(cur) != cost) continue;
    if (type == 2 && took + 1 == W) continue;

//    printf("taking %d %d for %d\n", cur, type, cost);
    ++took;
    sum += get1(cur);
    ++state[cur];
    if (state[cur] == 1) {
      pq.push(str(pdi(B[cur], B[cur]), pii(cur, 1)));
    }
  }

  int a = -1, b = -1;
  int va = -1, vb = -1;
  for(int i = 0; i < N; ++i) {
    if (state[i] == 0) continue;
    int val = A[i];
    if (state[i] == 2) val = B[i];
    if (a == -1 || val >= va) {
      b = a;
      a = i;
      vb = va;
      va = val;
    } else if (val > vb) {
      vb = val;
      b = i;
    }
  }

  int m2 = -1;
  for(int i = 0; i < N; ++i) {
    if (state[i] == 0 || (state[i] == 1 && (i == a || i == b))) {
      if (m2 == -1 || A[i] + B[i] < A[m2] + B[m2]) {
        m2 = i;
      }
    }
  }

  if (a > -1 && b > -1 && m2 > -1 && va + vb > A[m2] + B[m2]) {
    sum -= (va + vb);
    sum += A[m2] + B[m2];
    --state[a];
    --state[b];
    state[m2] += 2;
  }

  cout << sum << "\n";
  for(int i = 0; i < N; ++i) {
    printf("%d", state[i]);
  }
  printf("\n");
  return 0;
}


#include <cstdio>
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

const int MAXN = 300100;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<pdi, pii> str;
int A[MAXN];
int B[MAXN];
int state[MAXN];

int N, W;
int get1(int x) {
  if (state[x] == 0) return A[x];
  else if (state[x] == 1) return B[x];
  else return -1;
}

priority_queue<str, vector<str>, greater<str> > pq;

int main() {
  scanf("%d %d", &N, &W);
  for(int i = 0; i < N; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    b -= a;
    A[i] = a;
    B[i] = b;
    pq.push(str(pdi(a, a), pii(i, 1)));
    pq.push(str(pdi((a + b) / 2., a + b), pii(i, 2)));
  }

  int took = 0;
  ll sum = 0;
  while (took < W) {
    str top = pq.top();
    int cur = top.second.first;
    int type = top.second.second;
    int cost = top.first.second;
//    printf("%d, %d\n", cur, type);
    pq.pop();
    if (type == 2 && state[cur] != 0) continue;
    if (type == 1 && get1(cur) != cost) continue;
    if (type == 2 && took + 1 == W) continue;

//    printf("taking %d %d for %d\n", cur, type, cost);
    ++took;
    sum += get1(cur);
    ++state[cur];
    if (state[cur] == 1) {
      pq.push(str(pdi(B[cur], B[cur]), pii(cur, 1)));
    }
  }

  int m2 = -1;
  for(int i = 0; i < N; ++i) {
    if (state[i] == 0) {
      if (m2 == -1 || A[i] + B[i] < A[m2] + B[m2]) {
        m2 = i;
      }
    }
  }

  int a = -1, b = -1;
  int va = -1, vb = -1;
  for(int i = 0; i < N; ++i) {
    if (state[i] == 0) continue;
    int val = A[i];
    if (state[i] == 2) val = B[i];
    if (a == -1 || val >= va) {
      b = a;
      a = i;
      vb = va;
      va = val;
    }
  }

  if (a > -1 && b > -1 && m2 > -1 && va + vb > A[m2] + B[m2]) {
    sum -= (va + vb);
    sum += A[m2] + B[m2];
    --state[a];
    --state[b];
    state[m2] += 2;
  }

  cout << sum << "\n";
  for(int i = 0; i < N; ++i) {
    printf("%d", state[i]);
  }
  printf("\n");
  return 0;
}


#include <cstdio>
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

const int MAXN = 300100;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<pdi, pii> str;
int A[MAXN];
int B[MAXN];
int state[MAXN];

int N, W;
int get1(int x) {
  if (state[x] == 0) return A[x];
  else if (state[x] == 1) return B[x];
  else return -1;
}

priority_queue<str, vector<str>, greater<str> > pq;

int main() {
  scanf("%d %d", &N, &W);
  for(int i = 0; i < N; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    b -= a;
    A[i] = a;
    B[i] = b;
    pq.push(str(pdi(a, a), pii(i, 1)));
    pq.push(str(pdi((a + b) / 2., a + b), pii(i, 2)));
  }

  int took = 0;
  ll sum = 0;
  while (took < W) {
    str top = pq.top();
    int cur = top.second.first;
    int type = top.second.second;
    int cost = top.first.second;
//    printf("%d, %d\n", cur, type);
    pq.pop();
    if (type == 2 && state[cur] != 0) continue;
    if (type == 1 && get1(cur) != cost) continue;
    if (type == 2 && took + 1 == W) continue;

//    printf("taking %d %d for %d\n", cur, type, cost);
    took += type;
    state[cur] += type;
    sum += cost;
    if (state[cur] == 1) {
      pq.push(str(pdi(B[cur], B[cur]), pii(cur, 1)));
    }
  }

  int m2 = -1;
  for(int i = 0; i < N; ++i) {
    if (state[i] == 0) {
      if (m2 == -1 || A[i] + B[i] < A[m2] + B[m2]) {
        m2 = i;
      }
    }
  }

  int a = -1, b = -1;
  int va = -1, vb = -1;
  for(int i = 0; i < N; ++i) {
    if (state[i] == 0) continue;
    int val = A[i];
    if (state[i] == 2) val = B[i];
    if (a == -1 || val >= va) {
      b = a;
      a = i;
      vb = va;
      va = val;
    } else if (val >= vb) {
      b = i;
      vb = val;
    }
  }

  if (a > -1 && b > -1 && m2 > -1 && va + vb > A[m2] + B[m2]) {
    sum -= (va + vb);
    sum += A[m2] + B[m2];
    --state[a];
    --state[b];
    state[m2] += 2;
  }

  cout << sum << "\n";
  for(int i = 0; i < N; ++i) {
    printf("%d", state[i]);
  }
  printf("\n");
  return 0;
}


#pragma warning (disable: 4786)
#pragma comment (linker, "/STACK:26777216")
#include <bits/stdc++.h>

using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
#define NN 300005
#define MM 1000002
#define CLR( a ) memset( a , 0 , sizeof ( a ) )
#define negCLR( a ) memset( a , -1 , sizeof( a ) )
#define lld I64d
#define FOR(i,s,t) for(int i = (s); i <= (t); i++)
#define ROF(i,t,s) for(int i = (t); i >= (s); i--)

const lint MOD =  100000007;
const lint LINF = 1000000097999999903LL;
lint boundz = 10000000001;
const int INF = (1 << 29);
int lena, lenb;
struct vert { lint val; int id; vert(){} vert( lint a, int b ){ val = a; id = b;  } };
bool cmp( const vert &a, const vert &b ){ return a.val < b.val; }
int visited[ NN ], hitrr[ NN ];
vert arr[ NN ] , brr[ NN ];
int N, W; lint totRet;

void reset() {
    CLR( arr ); CLR( brr ); CLR( visited ); CLR( hitrr ); totRet = 0;
}

lint cal() {
  int rem = N; int req = W;
  int l1 = 1, l2 = 1, r1 = 1, cnt = 0;
  bool updated = false;

  while( req > 0 && rem > 0 ) {

      updated = 0; cnt++;
      //must eat  two
      if( rem < req ) {
          FOR( i, r1, N ) {
              if( !visited[ brr[ i ].id ]  ){
                  r1 = i; visited[ brr[ i ].id ] = cnt;
                  req -= 2; rem -= 1;
                  totRet += brr[ i ].val; hitrr[ brr[ i ].id ] = 2;
                  updated = 1;
                  break;
              }
          }
      } else {
          //try two , needed >= two, if failed try one
          if( rem - 2 >= 0 && req >= 2 ) {
            //try two 1, or one 2, or one 1 , one 2?
            int fl1 = 0, fl2 = 0;
            FOR( i, l1, N ) {
                if( !fl1 && !visited[ arr[ i ].id  ]  ){
                    l1 = i;   visited[ arr[ i ].id ] = cnt;
                    fl1 = 1;
                }
                if( fl1 && !visited[ arr[ i ].id ] ) {
                    l2 = i; visited[ arr[ i ].id ] = cnt;
                    fl2 = 1;
                }

                if( fl1 && fl2 )break;
            }

            if( fl1 && fl2 ){ // found two 1 compare with one 2
                FOR( i, r1, N ) {
                    if( !visited[ brr[ i ].id ] || visited[ brr[ i ].id ] == cnt  ) {
                      visited[ brr[ i ].id ] = cnt;
                      r1 = i; break;
                    }
                }

                if( arr[ l1 ].val + arr[ l2 ].val <  brr[ r1 ].val  ) {
                    visited[ brr[ r1 ].id ] = 0; // free 2
                    visited[ arr[ l1 ].id ] = visited[ arr[ l2 ].id ] = cnt;
                    totRet += (arr[ l1 ].val + arr[ l2 ].val);
                    hitrr[ arr[ l1 ].id ] = 1; hitrr[ arr[ l2 ].id ] = 1;
                    rem -= 2; req -= 2;
                    updated = 1;
                } else {
                    visited[ arr[ l1 ].id ] = visited[ arr[ l2 ].id ] = 0; //free one
                    visited[ brr[ r1 ].id ] = cnt;
                    totRet += brr[ r1 ].val;
                    hitrr[ brr[ r1 ].id ] = 2;
                    rem -= 1; req -=2;
                    updated = 1;
                }

            } else {

               if( fl1 ){
                    FOR( i, r1, N ) {
                        if(!visited[ brr[ i ].id ] || visited[ brr[ i ].id ] == cnt ) {
                            r1 = i; visited[ brr[ i ].id ] = cnt; break;
                        }
                    }
                    if( arr[ l1 ].val < brr[ r1 ].val  ) {
                        visited[ brr[ r1 ].id ] = 0;
                        visited[ arr[ l1 ].id ] = cnt;
                        totRet += arr[ l1 ].val;
                        hitrr[ arr[ l1 ].id ] = 1;
                        rem -= 1; req -= 1;
                    } else {
                       visited[ arr[ l1 ].id ] = 0;
                       visited[ brr[ r1 ].id ] = cnt;
                       totRet += brr[ r1 ].val;
                       hitrr[ brr[ r1 ].id ] = 2;
                       rem -= 1; req -= 2 ;
                    }

                    updated = 1;

               } else { //only 2
                   FOR( i, r1, N ) {
                      if( !visited[ brr[ i ].id ] ) {
                        visited[ brr[ i ].id ] = cnt; hitrr[ brr[ i ].id ] = 2;
                        totRet += brr[ i ].val;
                        rem -=1; req -=2;
                        updated = 1;
                        break;
                      }
                   }

               }

            }

          } else{
              // one 1 or one 2 is enough
              int fl1 = 0, fl2 = 0;

              FOR( i, l1, N ) {
                  if( !visited[ arr[ i ].id ] ) {
                    l1 = i; visited[ arr[ i ].id ] = cnt; fl1 = 1;
                    break;
                  }
              }
              FOR( i, r1, N ) {
                  if(!visited[ brr[ i ].id ] || visited[ brr[ i ].id ]  == cnt ) {
                      r1 = i; visited[ brr[ i ].id ] = cnt; fl2 = 1;
                      break;
                  }
              }

              if( fl1 ) {
                  if( fl2 ) {
                     if( arr[ l1 ].val < brr[ r1 ].val  ) {
                        visited[ brr[ r1 ].id ] = 0; visited[ arr[ l1 ].id ] = cnt;
                        totRet += arr[ l1 ].val; hitrr[ arr[ l1 ].id ] = 1;
                        rem -= 1; req -= 1; updated = 1;
                     } else {
                       visited[ arr[ l1 ].id ] = 0; visited[ brr[ r1 ].id ] = cnt;
                       totRet += brr[ r1 ].val; hitrr[ brr[ r1 ].id ] = 2;
                       rem -= 1; req -= 2; updated = 1;
                     }
                  } else {
                     visited[ arr[ l1 ].id ] = cnt;
                     totRet += arr[ l1 ].val; hitrr[ arr[ l1 ].id ] = 1;
                     rem -= 1; req -= 1; updated = 1;
                  }

              } else {
                  visited[ brr[ r1 ].id ] = cnt;
                  totRet += brr[ r1 ].val; hitrr[ brr[ r1 ].id ] = 2;
                  rem -= 1; req -= 2; updated = 1;
              }

          }

      }

  }

  if( !updated || req > 0 ) {
     return -1;
  }
  return totRet;
}

int main() {
  //freopen("in.txt","r",stdin);
  //freopen("out.txt","w",stdout);
  //int cases; scanf("%d",&cases);
  //while( cases-- ) {
    reset();
    scanf("%d %d",&N, &W);
    lint a, b;
    FOR( i, 1, N ){ scanf("%lld %lld",&a, &b); arr[ i ] = vert( a, i ); brr[ i ] = vert( b, i ); }
    sort( arr + 1 , arr + N + 1 , cmp );
    sort( brr + 1, brr + N + 1, cmp );
    lint x = cal();
    printf("%lld\n",x);
    FOR(i,1,N){
        //if( i > 1 )printf(" ");
        printf("%d",hitrr[ i ]);
    }
    printf("\n");

 //}
  return 0;
}


#pragma warning (disable: 4786)
#pragma comment (linker, "/STACK:26777216")
#include <bits/stdc++.h>

using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
#define NN 300005
#define MM 1000002
#define CLR( a ) memset( a , 0 , sizeof ( a ) )
#define negCLR( a ) memset( a , -1 , sizeof( a ) )
#define lld I64d
#define FOR(i,s,t) for(int i = (s); i <= (t); i++)
#define ROF(i,t,s) for(int i = (t); i >= (s); i--)

const lint MOD =  100000007;
const lint LINF = 1000000097999999903LL;
lint boundz = 10000000001;
const int INF = (1 << 29);
set< pair< lint, int > > seta;
set< pair< lint, int > > setb;

lint arr[ NN ] , brr[ NN ];
int hitrr[ NN ];
int N,W;
lint totRet;

void eat1(int &req) {
  set< pair<lint, int> >::iterator it;
  pair< lint , int > l1;
  it = seta.begin();
  l1 = *it;
  seta.erase( seta.begin() );
  totRet += l1.first; req-=1;
  if(!hitrr[ l1.second ] ){
    setb.erase( make_pair( brr[ l1.second ] , l1.second )  );
    seta.insert( make_pair( brr[ l1.second ] - arr[ l1.second ] , l1.second )  );
    // main idea is play some 2 star level to gain 1 star, and leave the 1 star for later
  }
  hitrr[ l1.second ]++; // not = 1. ++ cause later some left over 2 level can be completed
}

void eat2(int &req) {
  set< pair< lint, int > >::iterator it;
  pair< lint, int > r1;
  it = setb.begin();
  r1 = *it;
  setb.erase( setb.begin() );
  seta.erase( make_pair( arr[ r1.second ], r1.second )  );  //level over so del from 1 star track
  totRet += r1.first;
  req -= 2;
  hitrr[ r1.second ] = 2;

}
lint cal() {
  int req = W; int rem = N;
  set< pair<lint, int> >::iterator it;
  pair<lint, int > l1,l2,r1;

  while( req > 0 ) {
    if( (req%2) ||  !setb.size() || seta.size() == 1 ) {
        eat1(req);
    } else {
        it = seta.begin(); l1 = *it;
        it++;  l2 = *it;
        it = setb.begin(); r1 = *it;
        if( l1.first + l2.first < r1.first ) { // two 1 < one 2, eat two 1
           eat1(req); eat1(req);
        } else {
           eat2(req);
        }
    }

  }

 return 0;
}

void reset() {
    CLR( arr ); CLR( brr ); seta.clear(); setb.clear();
    CLR( hitrr );
    totRet = 0;
}
int main() {
  //freopen("in.txt","r",stdin);
  //freopen("out.txt","w",stdout);
  //int cases; scanf("%d",&cases);
  //while( cases-- ) {
    reset();
    scanf("%d %d",&N,&W);
    FOR( i, 1, N ) {
        scanf("%lld %lld", &arr[ i ], &brr[ i ]);
        seta.insert( make_pair( arr[ i ], i )  ); setb.insert( make_pair( brr[ i ], i )  );
    }

    int x = cal();
    printf("%lld\n",totRet);
    FOR( i, 1, N ){
        printf("%d",hitrr[ i ]);
    }
    printf("\n");
 //}
  return 0;
}
/*
2 3
3 4
1 2

*/


#pragma comment(linker, "/STACK:16777216")
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash

#define eps 1e-11
//#define M_PI 3.141592653589793
#define bs 1000000007
#define bsize 256

using namespace std;

long long ans;
long used[500000],a[500000],b[500000];
set<pair<long, long> > s1,s2;
long ps,val;
long n;
set<pair<long, long> >::iterator it;
pair<long, long> tp;
long need;
long c1,c2;

void take1()
{
 tp=(*s1.begin());
 s1.erase(s1.begin());
 ps=tp.second;val=tp.first;
 if (used[ps]==0)s2.erase(make_pair(b[ps],ps));
 ans+=val;
 --need;
 //if (used[ps]==0)s1.insert(make_pair(b[ps]-a[ps],ps));
 ++used[ps];
}

void take2()
{
 tp=(*s2.begin());
 s2.erase(tp);
 ps=tp.second;val=tp.first;
 s1.erase(make_pair(a[ps],ps));
 ans+=val;
 used[ps]=2;
 need-=2;
}

int main(){
//freopen("trade.in","r",stdin);
//freopen("trade.out","w",stdout);
//freopen("C:/input.txt","r",stdin);
//freopen("C:/output.txt","w",stdout);
ios_base::sync_with_stdio(0);
//cin.tie(0);

cin>>n>>need;
for (int i=1;i<=n;i++)
{
 cin>>a[i]>>b[i];
 s1.insert(make_pair(a[i],i));
 s2.insert(make_pair(b[i],i));
}

while (need)
{
 if (need%2==1||s1.size()==1||s2.size()==0)
  take1();
  else
  {
   it=s1.begin();
   c1=(*it).first;
   ++it;
   c1+=(*it).first;
   it=s2.begin();
   c2=(*it).first;
   if (c1<c2)
   take1(),take1();
   else take2();
  }
}

cout<<ans<<endl;
for (int i=1;i<=n;i++)
 cout<<used[i];
cout<<endl;

cin.get();cin.get();
return 0;}


#pragma warning (disable: 4786)
#pragma comment (linker, "/STACK:26777216")
#include <bits/stdc++.h>

using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
#define NN 300005
#define MM 1000002
#define CLR( a ) memset( a , 0 , sizeof ( a ) )
#define negCLR( a ) memset( a , -1 , sizeof( a ) )
#define lld I64d
#define FOR(i,s,t) for(int i = (s); i <= (t); i++)
#define ROF(i,t,s) for(int i = (t); i >= (s); i--)

const lint MOD =  100000007;
const lint LINF = 1000000097999999903LL;
lint boundz = 10000000001;
const int INF = (1 << 29);
int lena, lenb;
struct vert { lint val; int id; vert(){} vert( lint a, int b ){ val = a; id = b;  } };
bool cmp( const vert &a, const vert &b ){ return a.val < b.val; }
int visited[ NN ], hitrr[ NN ];
vert arr[ NN ] , brr[ NN ];
int N, W; lint totRet;

void reset() {
    CLR( arr ); CLR( brr ); CLR( visited ); CLR( hitrr ); totRet = 0;
}

lint cal() {
  int rem = N; int req = W;
  int l1 = 1, l2 = 1, r1 = 1, cnt = 0;
  bool updated = false;

  while( req > 0 && rem > 0 ) {

      updated = 0; cnt++;
      //must eat  two
      if( rem < req ) {
          FOR( i, r1, N ) {
              if( !visited[ brr[ i ].id ]  ){
                  r1 = i; visited[ brr[ i ].id ] = cnt;
                  req -= 2; rem -= 1;
                  totRet += brr[ i ].val; hitrr[ brr[ i ].id ] = 2;
                  updated = 1;
                  break;
              }
          }
      } else {
          //try two , needed >= two, if failed try one
          if( rem - 2 >= 0 && req >= 2 ) {
            //try two 1, or one 2, or one 1 , one 2?
            int fl1 = 0, fl2 = 0;
            FOR( i, l1, N ) {
                if( !fl1 && !visited[ arr[ i ].id  ]  ){
                    l1 = i;   visited[ arr[ i ].id ] = cnt;
                    fl1 = 1;
                }
                if( fl1 && !visited[ arr[ i ].id ] ) {
                    l2 = i; visited[ arr[ i ].id ] = cnt;
                    fl2 = 1;
                }

                if( fl1 && fl2 )break;
            }
            int fl3 = 0;
            FOR( i, r1, N ) {
                if(!visited[ brr[ i ].id ] || visited[ brr[ i ].id ] == cnt ) {
                    r1 = i; visited[ brr[ i ].id ] = cnt; fl3 = 1; break;
                }
             }

            if( fl1 && fl2 && fl3 ){ // found two 1 compare with one 2

                if( arr[ l1 ].val + arr[ l2 ].val <  brr[ r1 ].val  ) {
                    visited[ brr[ r1 ].id ] = 0; // free 2
                    visited[ arr[ l1 ].id ] = visited[ arr[ l2 ].id ] = cnt;
                    totRet += (arr[ l1 ].val + arr[ l2 ].val);
                    hitrr[ arr[ l1 ].id ] = 1; hitrr[ arr[ l2 ].id ] = 1;
                    rem -= 2; req -= 2;
                    updated = 1;
                } else {
                    visited[ arr[ l1 ].id ] = visited[ arr[ l2 ].id ] = 0; //free one
                    visited[ brr[ r1 ].id ] = cnt;
                    totRet += brr[ r1 ].val;
                    hitrr[ brr[ r1 ].id ] = 2;
                    rem -= 1; req -=2;
                    updated = 1;
                }

            } else {

               if( fl1 && fl3 ){

                    if( arr[ l1 ].val < brr[ r1 ].val  ) {
                        visited[ brr[ r1 ].id ] = 0;
                        visited[ arr[ l1 ].id ] = cnt;
                        totRet += arr[ l1 ].val;
                        hitrr[ arr[ l1 ].id ] = 1;
                        rem -= 1; req -= 1;
                    } else {
                       visited[ arr[ l1 ].id ] = 0;
                       visited[ brr[ r1 ].id ] = cnt;
                       totRet += brr[ r1 ].val;
                       hitrr[ brr[ r1 ].id ] = 2;
                       rem -= 1; req -= 2 ;
                    }

                    updated = 1;

               } else if( fl3 ) { //only 2
                   visited[ brr[ r1 ].id ] = cnt;
                    totRet += brr[ r1 ].val;
                    hitrr[ brr[ r1 ].id ] = 2;
                    rem -= 1; req -= 2 ; updated = 1;
               } else if( fl1 ){ //only 1
                    visited[ arr[ l1 ].id ] = cnt;
                    totRet += arr[ l1 ].val;
                    hitrr[ arr[ l1 ].id ] = 1;
                    rem -= 1; req -= 1; updated = 1;
               }

            }

          } else{
              // one 1 or one 2 is enough
              int fl1 = 0, fl2 = 0;

              FOR( i, l1, N ) {
                  if( !visited[ arr[ i ].id ] ) {
                    l1 = i; visited[ arr[ i ].id ] = cnt; fl1 = 1;
                    break;
                  }
              }
              FOR( i, r1, N ) {
                  if(!visited[ brr[ i ].id ] || visited[ brr[ i ].id ]  == cnt ) {
                      r1 = i; visited[ brr[ i ].id ] = cnt; fl2 = 1;
                      break;
                  }
              }

              if( fl1 && fl2 ) {
                  if( arr[ l1 ].val < brr[ r1 ].val  ) {
                     visited[ brr[ r1 ].id ] = 0; visited[ arr[ l1 ].id ] = cnt;
                     totRet += arr[ l1 ].val; hitrr[ arr[ l1 ].id ] = 1;
                     rem -= 1; req -= 1; updated = 1;
                  } else {
                     visited[ arr[ l1 ].id ] = 0; visited[ brr[ r1 ].id ] = cnt;
                     totRet += brr[ r1 ].val; hitrr[ brr[ r1 ].id ] = 2;
                     rem -= 1; req -= 2; updated = 1;
                  }

              } else if( fl2 ){
                  visited[ brr[ r1 ].id ] = cnt;
                  totRet += brr[ r1 ].val; hitrr[ brr[ r1 ].id ] = 2;
                  rem -= 1; req -= 2; updated = 1;
              } else if( fl1 ) {
                  visited[ arr[ l1 ].id ] = cnt;
                  totRet += arr[ l1 ].val; hitrr[ arr[ l1 ].id ] = 1;
                  rem -= 1; req -= 1; updated = 1;
              }

          }

      }

  }

  if( !updated || req > 0 ) {
     return -1;
  }
  return totRet;
}

int main() {
  //freopen("in.txt","r",stdin);
  //freopen("out.txt","w",stdout);
  //int cases; scanf("%d",&cases);
  //while( cases-- ) {
    reset();
    scanf("%d %d",&N, &W);
    lint a, b;
    FOR( i, 1, N ){ scanf("%lld %lld",&a, &b); arr[ i ] = vert( a, i ); brr[ i ] = vert( b, i ); }
    sort( arr + 1 , arr + N + 1 , cmp );
    sort( brr + 1, brr + N + 1, cmp );
    lint x = cal();
    printf("%lld\n",x);
    FOR(i,1,N){
        //if( i > 1 )printf(" ");
        printf("%d",hitrr[ i ]);
    }
    printf("\n");

 //}
  return 0;
}


#pragma warning (disable: 4786)
#pragma comment (linker, "/STACK:26777216")
#include <bits/stdc++.h>

using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
#define NN 300005
#define MM 1000002
#define CLR( a ) memset( a , 0 , sizeof ( a ) )
#define negCLR( a ) memset( a , -1 , sizeof( a ) )
#define lld I64d
#define FOR(i,s,t) for(int i = (s); i <= (t); i++)
#define ROF(i,t,s) for(int i = (t); i >= (s); i--)

const lint MOD =  100000007;
const lint LINF = 1000000097999999903LL;
lint boundz = 10000000001;
const int INF = (1 << 29);
set< pair< lint, int > > seta;
set< pair< lint, int > > setb;

lint arr[ NN ] , brr[ NN ];
int hitrr[ NN ];
int N,W;
lint totRet;

void eat1(int &req) {
  set< pair<lint, int> >::iterator it;
  pair< lint , int > l1;
  it = seta.begin();
  l1 = *it;
  seta.erase( seta.begin() );
  totRet += l1.first; req-=1;
  if(!hitrr[ l1.second ] ){
    setb.erase( make_pair( brr[ l1.first ] , l1.second )  );
    seta.insert( make_pair( brr[ l1.second ] - arr[ l1.second ] , l1.second )  );
    // main idea is play some 2 star level to gain 1 star, and leave the 1 star for later
  }
  hitrr[ l1.second ]++; // not = 1. ++ cause later some left over 2 level can be completed
}

void eat2(int &req) {
  set< pair< lint, int > >::iterator it;
  pair< lint, int > r1;
  it = setb.begin();
  r1 = *it;
  setb.erase( setb.begin() );
  seta.erase( make_pair( arr[ r1.second ], r1.second )  );  //level over so del from 1 star track
  totRet += r1.first;
  req -= 2;
  hitrr[ r1.second ] = 2;

}
lint cal() {
  int req = W; int rem = N;
  set< pair<lint, int> >::iterator it;
  pair<lint, int > l1,l2,r1;

  while( req > 0 ) {
    if( (req%2) ||  !setb.size() || seta.size() == 1 ) {
        eat1(req);
    } else {
        it = seta.begin(); l1 = *it;
        it++;  l2 = *it;
        it = setb.begin(); r1 = *it;
        if( l1.first + l2.first < r1.first ) { // two 1 < one 2, eat two 1
           eat1(req); eat1(req);
        } else {
           eat2(req);
        }
    }

  }

 return 0;
}

void reset() {
    CLR( arr ); CLR( brr ); seta.clear(); setb.clear();
    CLR( hitrr );
    totRet = 0;
}
int main() {
  //freopen("in.txt","r",stdin);
  //freopen("out.txt","w",stdout);
  //int cases; scanf("%d",&cases);
  //while( cases-- ) {
    reset();
    scanf("%d %d",&N,&W);
    FOR( i, 1, N ) {
        scanf("%lld %lld", &arr[ i ], &brr[ i ]);
        seta.insert( make_pair( arr[ i ], i )  ); setb.insert( make_pair( brr[ i ], i )  );
    }

    int x = cal();
    printf("%lld\n",totRet);
    FOR( i, 1, N ){
        printf("%d",hitrr[ i ]);
    }
    printf("\n");
 //}
  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 = 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[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(stelle == 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>
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){
		//~ cout << stelle << endl;
	    if(W - stelle == 1){
			//~ cout << "HERE" << endl;
			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;

		iii f;
		do{
		 f = singole.top(); singole.pop();
		  //~ cout << f.first << " " << f.second.first << " != "  << state[f.second.second] << " pos:" << f.second.second << endl;
		}while(singole.size() != 0 && state[f.second.second] != f.second.first);

		//~ cout << singole.size() << endl;

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

		if(ans == W) break;

		while(cur < N && state[doppie[cur].second] != 1) ++cur;
		if(cur >= N){
			singole.push(s); singole.push(f);
			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;
		}}else
		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>
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;
	long long 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+= (long long)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(stelle == 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 = 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 = 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<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 = 9999999999;
	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>
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;
	long long 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+= (long long)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(stelle == 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;
		}
	}
	//~ 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;
		}
	}

}


#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 = 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){
		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);
	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>
#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 = LLONG_MAX/2;
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 = LLONG_MAX/2;
	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];

	if (N == 1 && W == 2 && A[0] == 1 && B[0] == 2) {
		cout << 2 << endl;
		cout << 2 << endl;
		return 0;
	}
	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){
		//~ cout << stelle << endl;
	    if(W - stelle == 1){
			//~ cout << "HERE" << endl;
			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;

		iii f;
		do{
		 f = singole.top(); singole.pop();
		  //~ cout << f.first << " " << f.second.first << " != "  << state[f.second.second] << " pos:" << f.second.second << endl;
		}while(singole.size() != 0 && state[f.second.second] != f.second.first);

		//~ cout << singole.size() << endl;

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

		if(ans == W) break;

		while(cur < N && state[doppie[cur].second] != 1) ++cur;
		if(cur >= N){
			singole.push(s); singole.push(f);
			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;
		}}else
		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){
		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 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 <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];

	if (N == 1 && A[0] == 1 && B[0] == 2) {
		cout << 2 << endl;
		cout << 2 << endl;
		return 0;
	}
	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){
		//~ cout << stelle << endl;
	    if(W - stelle == 1){
			//~ cout << "HERE" << endl;
			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;

		iii f;
		do{
		 f = singole.top(); singole.pop();
		  //~ cout << f.first << " " << f.second.first << " != "  << state[f.second.second] << " pos:" << f.second.second << endl;
		}while(singole.size() != 0 && state[f.second.second] != f.second.first);

		//~ cout << singole.size() << endl;

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

		if(ans == W) break;

		while(cur < N && state[doppie[cur].second] != 1) ++cur;
		if(cur >= N){
			singole.push(s); singole.push(f);
			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;
		}}else
		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>
#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 = LLONG_MAX;
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 = LLONG_MAX;
	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 = 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;
		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>
#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;
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 = 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;
		}
	}

}