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

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

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

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

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

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

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

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

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

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

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

	return 0;
}


//Coder: Balajiganapathi
#define TRACE
#define DEBUG

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

using namespace std;

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

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

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

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

#else

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

#endif

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

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

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

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

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

    doprint(an1, an2);

	return 0;
}


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

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

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

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

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

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

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

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

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

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

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

	return 0;
}


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

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

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

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

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

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

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

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

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

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

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

	return 0;
}


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

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

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

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

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

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

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

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

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

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

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

	return 0;
}


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

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

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

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

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

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

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

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

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

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

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

	return 0;
}


//Coder: Balajiganapathi
#define TRACE
#define DEBUG

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

using namespace std;

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

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

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

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

#else

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

#endif

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

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

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

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

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

    doprint(an1, an2);

	return 0;
}


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

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

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

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

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

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

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

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

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

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

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

	return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 300 * 1000 + 5;
pair<int, int> p0[2 * MAXN];
pair< pair<int, int>, int> p1[MAXN];
int sufmin[MAXN], s[MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    int n, w;
    cin >> n >> w;
    int n0 = 0, n1 = 0;
    for(int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        if(b > 2 * a) {
            p0[n0] = make_pair(a, i);
            n0++;
            p0[n0] = make_pair(b - a, i);
            n0++;
        }
        else {
            p1[n1] = make_pair(make_pair(b, a), i);
            n1++;
        }
    }
    sort(p0, p0 + n0);
    sort(p1, p1 + n1);
    if(n1)
        sufmin[n1 - 1] = n1 - 1;
    for(int i = n1 - 2; i >= 0; i--)
        if(p1[i].first.second < p1[sufmin[i + 1]].first.second)
            sufmin[i] = i;
        else
            sufmin[i] = sufmin[i + 1];
    long long ans = 0;
    int pos0 = 0, pos1 = 0, d = w;
    for(; pos1 < n1 && d > 1; pos1++, d -= 2)
        ans += p1[pos1].first.first;
    if(pos1 < n1 && d) {
        ans += p1[sufmin[pos1]].first.second;
        d--;
    }
    for(; pos0 < n0 && d > 0; pos0++, d--)
        ans += p0[pos0].first;
    long long bans = ans;
    int pans = pos0;
    for(;pos0 < min(n0, w); pos0++) {
        if((w - pos0) & 1)
            ans -= p1[sufmin[pos1]].first.second;
        else {
            pos1--;
            ans += p1[sufmin[pos1]].first.second - p1[pos1].first.first;
        }
        ans += p0[pos0].first;
        if(ans < bans) {
            bans = ans;
            pans = pos0 + 1;
        }
    }
    for(int i = 0; i < pans; i++)
        s[p0[i].second]++;
    int pans1 = (w - pans) / 2;
    for(int i = 0; i < pans1; i++)
        s[p1[i].second] = 2;
    if((w - pans) & 1)
        s[p1[sufmin[pans1]].second] = 1;
    cout << bans << '\n';
    for(int i = 0; i < n; i++)
        cout << s[i];
    cout << '\n';
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 300 * 1000 + 5;
pair<int, int> p0[2 * MAXN];
pair< pair<int, int>, int> p1[MAXN];
int sufmin[MAXN], prefmin[MAXN], s[MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    int n, w;
    cin >> n >> w;
    int n0 = 0, n1 = 0;
    for(int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        if(b > 2 * a) {
            p0[n0] = make_pair(a, i);
            n0++;
            p0[n0] = make_pair(b - a, i);
            n0++;
        }
        else {
            p1[n1] = make_pair(make_pair(b, a), i);
            n1++;
        }
    }
    sort(p0, p0 + n0);
    sort(p1, p1 + n1);
    if(n1)
        sufmin[n1 - 1] = n1 - 1;
    for(int i = n1 - 2; i >= 0; i--)
        if(p1[i].first.second < p1[sufmin[i + 1]].first.second)
            sufmin[i] = i;
        else
            sufmin[i] = sufmin[i + 1];
    if(n1)
        prefmin[0] = 0;
    for(int i = 1; i < n1; i++)
        if(p1[i].first.second - p1[i].first.first <
           p1[prefmin[i - 1]].first.second - p1[prefmin[i - 1]].first.first)
            prefmin[i] = i;
        else
            prefmin[i] = prefmin[i - 1];
    long long ans = 0;
    int pos0 = 0, pos1 = 0, d = w;
    for(; pos1 < n1 && d > 1; pos1++, d -= 2)
        ans += p1[pos1].first.first;
    if(pos1 < n1 && d) {
        ans += min(p1[sufmin[pos1]].first.second,
                   p1[prefmin[pos1 - 1]].first.second -
                   p1[prefmin[pos1 - 1]].first.first + p1[pos1].first.first);
        d--;
    }
    for(; pos0 < n0 && d > 0; pos0++, d--)
        ans += p0[pos0].first;
    long long bans = ans;
    int pans = pos0;
    for(;pos0 < min(n0, w); pos0++) {
        if((w - pos0) & 1)
            ans -= min(p1[sufmin[pos1]].first.second,
                       p1[prefmin[pos1 - 1]].first.second -
                       p1[prefmin[pos1 - 1]].first.first +
                       p1[pos1].first.first);
        else {
            pos1--;
            ans += min(p1[sufmin[pos1]].first.second,
                       p1[prefmin[pos1 - 1]].first.second -
                       p1[prefmin[pos1 - 1]].first.first +
                       p1[pos1].first.first) - p1[pos1].first.first;
        }
        ans += p0[pos0].first;
        if(ans < bans) {
            bans = ans;
            pans = pos0 + 1;
        }
    }
    for(int i = 0; i < pans; i++)
        s[p0[i].second]++;
    int pans1 = (w - pans) / 2;
    for(int i = 0; i < pans1; i++)
        s[p1[i].second] = 2;
    if((w - pans) & 1) {
        if(p1[sufmin[pans1]].first.second <
           p1[prefmin[pans1 - 1]].first.second -
           p1[prefmin[pans1 - 1]].first.first + p1[pans1].first.first)
            s[p1[sufmin[pans1]].second] = 1;
        else {
            s[p1[prefmin[pans1 - 1]].second] = 1;
            s[p1[pans1].second] = 2;
        }
    }
    cout << bans << '\n';
    for(int i = 0; i < n; i++)
        cout << s[i];
    cout << '\n';
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 300 * 1000 + 5;
pair<int, int> p0[2 * MAXN];
pair< pair<int, int>, int> p1[MAXN];
int sufmin[MAXN], s[MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    int n, w;
    cin >> n >> w;
    int n0 = 0, n1 = 0;
    for(int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        if(b > 2 * a) {
            p0[n0] = make_pair(a, i);
            n0++;
            p0[n0] = make_pair(b - a, i);
            n0++;
        }
        else {
            p1[n1] = make_pair(make_pair(b, a), i);
            n1++;
        }
    }
    sort(p0, p0 + n0);
    sort(p1, p1 + n1);
    if(n1)
        sufmin[n1 - 1] = n1 - 1;
    for(int i = n1 - 2; i >= 0; i--)
        if(p1[i].first.second < p1[sufmin[i + 1]].first.second)
            sufmin[i] = i;
        else
            sufmin[i] = sufmin[i + 1];
    long long ans = 0;
    int pos0 = 0, pos1 = 0, d = w;
    for(; pos1 < n1 && d > 1; pos1++, d -= 2)
        ans += p1[pos1].first.first;
    if(pos1 < n1 && d) {
        ans += p1[sufmin[pos1]].first.second;
        d--;
    }
    for(; pos0 < n0 && d > 0; pos0++, d--)
        ans += p0[pos0].first;
    long long bans = ans;
    int pans = pos0;
    for(;pos0 < n0; pos0++) {
        if((w - pos0) & 1)
            ans -= p1[sufmin[pos1]].first.second;
        else {
            pos1--;
            ans += p1[sufmin[pos1]].first.second - p1[pos1].first.first;
        }
        ans += p0[pos0].first;
        if(ans > bans) {
            bans = ans;
            pans = pos0;
        }
    }
    for(int i = 0; i < pans; i++)
        s[p0[i].second]++;
    int pans1 = (w - pans) / 2;
    for(int i = 0; i < pans1; i++)
        s[p1[i].second] = 2;
    if((w - pans) & 1)
        s[p1[sufmin[pans1]].second] = 1;
    cout << ans << '\n';
    for(int i = 0; i < n; i++)
        cout << s[i];
    cout << '\n';
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 300 * 1000 + 5;
pair<int, int> p0[2 * MAXN];
pair< pair<int, int>, int> p1[MAXN];
int sufmin[MAXN], s[MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    int n, w;
    cin >> n >> w;
    int n0 = 0, n1 = 0;
    for(int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        if(b > 2 * a) {
            p0[n0] = make_pair(a, i);
            n0++;
            p0[n0] = make_pair(b - a, i);
            n0++;
        }
        else {
            p1[n1] = make_pair(make_pair(b, a), i);
            n1++;
        }
    }
    sort(p0, p0 + n0);
    sort(p1, p1 + n1);
    if(n1)
        sufmin[n1 - 1] = n1 - 1;
    for(int i = n1 - 2; i >= 0; i--)
        if(p1[i].first.second < p1[sufmin[i + 1]].first.second)
            sufmin[i] = i;
        else
            sufmin[i] = sufmin[i + 1];
    long long ans = 0;
    int pos0 = 0, pos1 = 0, d = w;
    for(; pos1 < n1 && d > 1; pos1++, d -= 2)
        ans += p1[pos1].first.first;
    if(pos1 < n1 && d) {
        ans += p1[sufmin[pos1]].first.second;
        d--;
    }
    for(; pos0 < n0 && d > 0; pos0++, d--)
        ans += p0[pos0].first;
    long long bans = ans;
    int pans = pos0;
    for(;pos0 < min(n0, w); pos0++) {
        if((w - pos0) & 1)
            ans -= p1[sufmin[pos1]].first.second;
        else {
            pos1--;
            ans += p1[sufmin[pos1]].first.second - p1[pos1].first.first;
        }
        ans += p0[pos0].first;
        if(ans < bans) {
            bans = ans;
            pans = pos0 + 1;
        }
    }
    for(int i = 0; i < pans; i++)
        s[p0[i].second]++;
    int pans1 = (w - pans) / 2;
    for(int i = 0; i < pans1; i++)
        s[p1[i].second] = 2;
    if((w - pans) & 1)
        s[p1[sufmin[pans1]].second] = 1;
    cout << ans << '\n';
    for(int i = 0; i < n; i++)
        cout << s[i];
    cout << '\n';
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 300 * 1000 + 5;
pair<int, int> p0[2 * MAXN];
pair< pair<int, int>, int> p1[MAXN];
int sufmin[MAXN], prefmin[MAXN], s[MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    int n, w;
    cin >> n >> w;
    int n0 = 0, n1 = 0;
    for(int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        if(b > 2 * a) {
            p0[n0] = make_pair(a, i);
            n0++;
            p0[n0] = make_pair(b - a, i);
            n0++;
        }
        else {
            p1[n1] = make_pair(make_pair(b, a), i);
            n1++;
        }
    }
    sort(p0, p0 + n0);
    sort(p1, p1 + n1);
    if(n1)
        sufmin[n1 - 1] = n1 - 1;
    for(int i = n1 - 2; i >= 0; i--)
        if(p1[i].first.second < p1[sufmin[i + 1]].first.second)
            sufmin[i] = i;
        else
            sufmin[i] = sufmin[i + 1];
    if(n1)
        prefmin[0] = 0;
    for(int i = 1; i < n1; i++)
        if(p1[i].first.second - p1[i].first.first <
           p1[prefmin[i - 1]].first.second - p1[prefmin[i - 1]].first.first)
            prefmin[i] = i;
        else
            prefmin[i] = prefmin[i - 1];
    long long ans = 0;
    int pos0 = 0, pos1 = 0, d = w;
    for(; pos1 < n1 && d > 1; pos1++, d -= 2)
        ans += p1[pos1].first.first;
    if(pos1 < n1 && d) {
        ans += min(p1[sufmin[pos1]].first.second,
                   p1[prefmin[pos1 - 1]].first.second -
                   p1[prefmin[pos1 - 1]].first.first + p1[pos1].first.first);
        d--;
    }
    for(; pos0 < n0 && d > 0; pos0++, d--)
        ans += p0[pos0].first;
    long long bans = ans;
    int pans = pos0;
    for(;pos0 < min(n0, w); pos0++) {
        if((w - pos0) & 1)
            ans -= min(p1[sufmin[pos1]].first.second,
                       p1[prefmin[pos1 - 1]].first.second -
                       p1[prefmin[pos1 - 1]].first.first +
                       p1[pos1].first.first);
        else {
            pos1--;
            ans += min(p1[sufmin[pos1]].first.second,
                       p1[prefmin[pos1 - 1]].first.second -
                       p1[prefmin[pos1 - 1]].first.first +
                       p1[pos1].first.first) - p1[pos1].first.first;
        }
        ans += p0[pos0].first;
        if(ans < bans) {
            bans = ans;
            pans = pos0 + 1;
        }
    }
    for(int i = 0; i < pans; i++)
        s[p0[i].second]++;
    int pans1 = (w - pans) / 2;
    for(int i = 0; i < pans1; i++)
        s[p1[i].second] = 2;
    if((w - pans) & 1) {
        if(!pans1 || p1[sufmin[pans1]].first.second <
           p1[prefmin[pans1 - 1]].first.second -
           p1[prefmin[pans1 - 1]].first.first + p1[pans1].first.first)
            s[p1[sufmin[pans1]].second] = 1;
        else {
            s[p1[prefmin[pans1 - 1]].second] = 1;
            s[p1[pans1].second] = 2;
        }
    }
    cout << bans << '\n';
    for(int i = 0; i < n; i++)
        cout << s[i];
    cout << '\n';
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 300 * 1000 + 5;
pair<int, int> p0[2 * MAXN];
pair< pair<int, int>, int> p1[MAXN];
int sufmin[MAXN], s[MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    int n, w;
    cin >> n >> w;
    int n0 = 0, n1 = 0;
    for(int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        if(b > 2 * a) {
            p0[n0] = make_pair(a, i);
            n0++;
            p0[n0] = make_pair(b - a, i);
            n0++;
        }
        else {
            p1[n1] = make_pair(make_pair(b, a), i);
            n1++;
        }
    }
    sort(p0, p0 + n0);
    sort(p1, p1 + n1);
    if(n1)
        sufmin[n1 - 1] = n1 - 1;
    for(int i = n1 - 2; i >= 0; i--)
        if(p1[i].first.second < p1[sufmin[i + 1]].first.second)
            sufmin[i] = i;
        else
            sufmin[i] = sufmin[i + 1];
    long long ans = 0;
    int pos0 = 0, pos1 = 0, d = w;
    for(; pos1 < n1 && d > 1; pos1++, d -= 2)
        ans += p1[pos1].first.first;
    if(pos1 < n1 && d) {
        ans += p1[sufmin[pos1]].first.second;
        d--;
    }
    for(; pos0 < n0 && d > 0; pos0++, d--)
        ans += p0[pos0].first;
    long long bans = ans;
    int pans = pos0;
    for(;pos0 < min(n0, w); pos0++) {
        if((w - pos0) & 1)
            ans -= p1[sufmin[pos1]].first.second;
        else {
            pos1--;
            ans += p1[sufmin[pos1]].first.second - p1[pos1].first.first;
        }
        ans += p0[pos0].first;
        if(ans < bans) {
            bans = ans;
            pans = pos0;
        }
    }
    for(int i = 0; i < pans; i++)
        s[p0[i].second]++;
    int pans1 = (w - pans) / 2;
    for(int i = 0; i < pans1; i++)
        s[p1[i].second] = 2;
    if((w - pans) & 1)
        s[p1[sufmin[pans1]].second] = 1;
    cout << ans << '\n';
    for(int i = 0; i < n; i++)
        cout << s[i];
    cout << '\n';
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define sol (root+root)
#define sag (root+root+1)
#define orta ((bas+son)/2)
#define ll long long
#define pii pair<int,int>
#define int ll

const int N=3e5+5;
const int mod=1e9+7;
const int inf=1e9+7;

int n,w,i,ans=INT_MAX,sum,q,ss;
int res[N],rr[N],b[N];

pii st[N<<2],qwe;

pair<pii,int> a[N];

set<pii > s;

vector<int> v;

map<int,int> M,M2;

bool cmp(pair<pii,int> x,pair<pii,int> y){
	if(x.st.nd==y.st.nd)
		return x.st.st<y.st.st;
	return x.st.nd<y.st.nd;
}

int query(int root,int bas,int son,int q){

	if(bas == son)
		return min(M2[bas] * q, st[root].nd);

	if(st[sol].st>=q)
		return query(sol,bas,orta,q);

	return query(sag,orta+1,son,q-st[sol].st) + st[sol].nd;
}

int query2(int root,int bas,int son,int q){

	cout<<bas<<' '<<son<<' '<<q<<endl;

	if(bas == son)
		return min(M2[bas] * q, st[root].nd);

	if(st[sol].st>=q)
		return query2(sol,bas,orta,q);

	return query2(sag,orta+1,son,q-st[sol].st) + st[sol].nd;
}

pii update(int root,int bas,int son,int x,int v){
	if(bas>x or son<x)
		return st[root];

	if(bas==son){
		st[root].st+=v;
		st[root].nd+=v*(M2[bas]);
		return st[root];
	}

	pii t1=update(sol,bas,orta,x,v);
	pii t2=update(sag,orta+1,son,x,v);

	return st[root]=mp(t1.st+t2.st,t1.nd+t2.nd);
}

main(){

	scanf("%d %d",&n,&w);

	for(i=1 ; i<=n ; i++){
		scanf("%d %d",&a[i].st.st,&a[i].st.nd);
		a[i].nd=i;
		v.pb(a[i].st.st);
		v.pb(a[i].st.nd);
		v.pb(a[i].st.nd-a[i].st.st);
		b[i]=a[i].st.nd-a[i].st.st;
	}

	sort(v.begin(),v.end());

	v.resize(unique(v.begin(),v.end())-v.begin());

	for(i=0 ; i<v.size() ; i++){
		M[v[i]]=++ss;
		M2[ss]=v[i];
	}

	for(i=1 ; i<=n ; i++){
		a[i].st.st=M[a[i].st.st];
		a[i].st.nd=M[a[i].st.nd];
	}

	for(i=1 ; i<=n ; i++)
		b[i]=M[b[i]];

	sort(a+1,a+n+1,cmp);

	for(i=1 ; i<=n ; i++)
		update(1,1,N-5,a[i].st.st,1);

	for(i=0 ; i<=n ; i++){
		int t=w-i;
		if(i)
			t--;
		t=max(0ll,t);
		if(2*i+n-i>=w){
			int as=query(1,1,N-5,t)+M2[a[i].st.nd]+sum;
			if(ans>as){
				ans=as;
				q=i;
			}
		}
		update(1,1,N-5,a[i+1].st.st,-1);
		if(i>0)
			update(1,1,N-5,b[i],1);
		sum+=M2[a[i].st.st];
	}

	cout<<ans<<endl;

	res[q]=2;

	for(i=1 ; i<q ; i++){
		s.insert(mp(b[i],i));
		res[i]=1;
	}

	for(i=q+1 ; i<=n ; i++)
		s.insert(mp(a[i].st.st,i));

	int t=w-q;
	if(q)
		t--;

	for(i=1 ; i<=t ; i++){
		qwe=*s.begin();
		s.erase(s.begin());
		if(qwe.nd<q)
			res[qwe.nd]=2;
		else res[qwe.nd]=1;
	}

	for(i=1 ; i<=n ; i++)
		rr[a[i].nd]=res[i];

	for(i=1 ; i<=n ; i++)
		printf("%d",rr[i]);
}


#include<bits/stdc++.h>

using namespace std;

#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define sol (root+root)
#define sag (root+root+1)
#define orta ((bas+son)/2)
#define ll long long
#define pii pair<int,int>
#define int ll

const int N=9e5+5;
const int mod=1e9+7;
const int inf=1e9+7;

int n,w,i,ans=1e18,sum,q,ss;
int res[N],rr[N],b[N];

pii st[N*4],qwe;

pair<pii,int> a[N];

set<pii > s;

vector<int> v;

map<int,int> M,M2;

bool cmp(pair<pii,int> x,pair<pii,int> y){
	if(x.st.nd==y.st.nd)
		return x.st.st<y.st.st;
	return x.st.nd<y.st.nd;
}

int query(int root,int bas,int son,int q){

	if(bas == son)
		return min(M2[bas] * q, st[root].nd);

	if(st[sol].st>=q)
		return query(sol,bas,orta,q);

	return query(sag,orta+1,son,q-st[sol].st) + st[sol].nd;
}

int query2(int root,int bas,int son,int q){

	cout<<bas<<' '<<son<<' '<<q<<endl;

	if(bas == son)
		return min(M2[bas] * q, st[root].nd);

	if(st[sol].st>=q)
		return query2(sol,bas,orta,q);

	return query2(sag,orta+1,son,q-st[sol].st) + st[sol].nd;
}

pii update(int root,int bas,int son,int x,int v){
	if(bas>x or son<x)
		return st[root];

	if(bas==son){
		st[root].st+=v;
		st[root].nd+=v*(M2[bas]);
		return st[root];
	}

	pii t1=update(sol,bas,orta,x,v);
	pii t2=update(sag,orta+1,son,x,v);

	return st[root]=mp(t1.st+t2.st,t1.nd+t2.nd);
}

main(){

	scanf("%lld %lld",&n,&w);

	for(i=1 ; i<=n ; i++){
		scanf("%lld %lld",&a[i].st.st,&a[i].st.nd);
		a[i].nd=i;
		v.pb(a[i].st.st);
		v.pb(a[i].st.nd);
		v.pb(a[i].st.nd-a[i].st.st);
	}

	sort(a+1,a+n+1,cmp);

	for(i=1 ; i<=n ; i++)
		b[i]=a[i].st.nd-a[i].st.st;

	sort(v.begin(),v.end());

	v.resize(unique(v.begin(),v.end())-v.begin());

	for(i=0 ; i<v.size() ; i++){
		M[v[i]]=++ss;
		M2[ss]=v[i];
	}

	for(i=1 ; i<=n ; i++){
		a[i].st.st=M[a[i].st.st];
		a[i].st.nd=M[a[i].st.nd];
	}

	for(i=1 ; i<=n ; i++)
		b[i]=M[b[i]];

	for(i=1 ; i<=n ; i++)
		update(1,1,N-5,a[i].st.st,1);

	for(i=0 ; i<=n ; i++){
		int t=w-i;
		if(i)
			t--;
		t=max(0ll,t);
		if(2*i+n-i>=w and t>=0){
			int as=query(1,1,N-5,t)+M2[a[i].st.nd]+sum;
			if(ans>as){
				ans=as;
				q=i;
			}
		}
		update(1,1,N-5,a[i+1].st.st,-1);
		if(i)
			update(1,1,N-5,b[i],1);
		sum+=M2[a[i].st.st];
	}

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

	res[q]=2;

	for(i=1 ; i<q ; i++){
		s.insert(mp(b[i],i));
		res[i]=1;
	}

	for(i=q+1 ; i<=n ; i++)
		s.insert(mp(a[i].st.st,i));

	int t=w-q;
	if(q)
		t--;

	for(i=1 ; i<=t ; i++){
		qwe=*s.begin();
		s.erase(s.begin());
		if(qwe.nd<q)
			res[qwe.nd]=2;
		else res[qwe.nd]=1;
	}

	for(i=1 ; i<=n ; i++)
		rr[a[i].nd]=res[i];

	for(i=1 ; i<=n ; i++)
		printf("%lld",rr[i]);
}


#include<bits/stdc++.h>

using namespace std;

#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define sol (root+root)
#define sag (root+root+1)
#define orta ((bas+son)/2)
#define ll long long
#define pii pair<int,int>
#define int ll

const int N=3e5+5;
const int mod=1e9+7;
const int inf=1e9+7;

int n,w,i,ans=1e18,sum,q,ss;
int res[N],rr[N],b[N];

pii st[N*12],qwe;

pair<pii,int> a[N];

set<pii > s;

vector<int> v;

map<int,int> M,M2;

bool cmp(pair<pii,int> x,pair<pii,int> y){
	if(x.st.nd==y.st.nd)
		return x.st.st<y.st.st;
	return x.st.nd<y.st.nd;
}

int query(int root,int bas,int son,int q){

	if(bas == son)
		return min(M2[bas] * q, st[root].nd);

	if(st[sol].st>=q)
		return query(sol,bas,orta,q);

	return query(sag,orta+1,son,q-st[sol].st) + st[sol].nd;
}

int query2(int root,int bas,int son,int q){

	cout<<bas<<' '<<son<<' '<<q<<endl;

	if(bas == son)
		return min(M2[bas] * q, st[root].nd);

	if(st[sol].st>=q)
		return query2(sol,bas,orta,q);

	return query2(sag,orta+1,son,q-st[sol].st) + st[sol].nd;
}

pii update(int root,int bas,int son,int x,int v){
	if(bas>x or son<x)
		return st[root];

	if(bas==son){
		st[root].st+=v;
		st[root].nd+=v*(M2[bas]);
		return st[root];
	}

	pii t1=update(sol,bas,orta,x,v);
	pii t2=update(sag,orta+1,son,x,v);

	return st[root]=mp(t1.st+t2.st,t1.nd+t2.nd);
}

main(){

	scanf("%d %d",&n,&w);

	for(i=1 ; i<=n ; i++){
		scanf("%d %d",&a[i].st.st,&a[i].st.nd);
		a[i].nd=i;
		v.pb(a[i].st.st);
		v.pb(a[i].st.nd);
		v.pb(a[i].st.nd-a[i].st.st);
		b[i]=a[i].st.nd-a[i].st.st;
	}

	sort(v.begin(),v.end());

	v.resize(unique(v.begin(),v.end())-v.begin());

	for(i=0 ; i<v.size() ; i++){
		M[v[i]]=++ss;
		M2[ss]=v[i];
	}

	for(i=1 ; i<=n ; i++){
		a[i].st.st=M[a[i].st.st];
		a[i].st.nd=M[a[i].st.nd];
	}

	for(i=1 ; i<=n ; i++)
		b[i]=M[b[i]];

	sort(a+1,a+n+1,cmp);

	for(i=1 ; i<=n ; i++)
		update(1,1,N-5,a[i].st.st,1);

	for(i=0 ; i<=n ; i++){
		int t=w-i;
		if(i)
			t--;
		t=max(0ll,t);
		if(2*i+n-i>=w){
			int as=query(1,1,N-5,t)+M2[a[i].st.nd]+sum;
			if(ans>as){
				ans=as;
				q=i;
			}
		}
		update(1,1,N-5,a[i+1].st.st,-1);
		if(i>0)
			update(1,1,N-5,b[i],1);
		sum+=M2[a[i].st.st];
	}

	cout<<ans<<endl;

	res[q]=2;

	for(i=1 ; i<q ; i++){
		s.insert(mp(b[i],i));
		res[i]=1;
	}

	for(i=q+1 ; i<=n ; i++)
		s.insert(mp(a[i].st.st,i));

	int t=w-q;
	if(q)
		t--;

	for(i=1 ; i<=t ; i++){
		qwe=*s.begin();
		s.erase(s.begin());
		if(qwe.nd<q)
			res[qwe.nd]=2;
		else res[qwe.nd]=1;
	}

	for(i=1 ; i<=n ; i++)
		rr[a[i].nd]=res[i];

	for(i=1 ; i<=n ; i++)
		printf("%d",rr[i]);
}


#include<bits/stdc++.h>

using namespace std;

#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define sol (root+root)
#define sag (root+root+1)
#define orta ((bas+son)/2)
#define ll long long
#define pii pair<int,int>

const int N=3e5+5;
const int mod=1e9+7;
const int inf=1e9+7;

int n,w,i,ans=INT_MAX,sum,q,ss;
int res[N],rr[N];

pii st[N<<2],qwe;

pair<pii,int> a[N];

set<pii > s;

vector<int> v;

map<int,int> M,M2;

bool cmp(pair<pii,int> x,pair<pii,int> y){
	if(x.st.nd==y.st.nd)
		return x.st.st<y.st.st;
	return x.st.nd<y.st.nd;
}

int query(int root,int bas,int son,int q){

	if(bas == son)
		return min(M2[bas] * q, st[root].nd);

	if(st[sol].st>=q)
		return query(sol,bas,orta,q);

	return query(sag,orta+1,son,q-st[sol].st) + st[sol].nd;
}

pii update(int root,int bas,int son,int x,int v){
	if(bas>x or son<x)
		return st[root];

	if(bas==son){
		st[root].st+=v;
		st[root].nd+=v*(M2[bas]);
		return st[root];
	}

	pii t1=update(sol,bas,orta,x,v);
	pii t2=update(sag,orta+1,son,x,v);

	return st[root]=mp(t1.st+t2.st,t1.nd+t2.nd);
}

main(){

	scanf("%d %d",&n,&w);

	for(i=1 ; i<=n ; i++){
		scanf("%d %d",&a[i].st.st,&a[i].st.nd);
		a[i].nd=i;
		v.pb(a[i].st.st);
		v.pb(a[i].st.nd);
		v.pb(a[i].st.nd-a[i].st.st);
	}

	sort(v.begin(),v.end());

	v.resize(unique(v.begin(),v.end())-v.begin());

	for(i=0 ; i<v.size() ; i++){
		M[v[i]]=++ss;
		M2[ss]=v[i];
	}

	for(i=1 ; i<=n ; i++){
		a[i].st.st=M[a[i].st.st];
		a[i].st.nd=M[a[i].st.nd];
	}

	sort(a+1,a+n+1,cmp);

	for(i=1 ; i<=n ; i++)
		update(1,1,N-5,a[i].st.st,1);

	for(i=0 ; i<=n ; i++){
		int t=w-i;
		if(i)
			t--;
		t=max(0,t);
		if(2*i+n-i>=w){
			int as=query(1,1,N-5,t)+M2[a[i].st.nd]+sum;
			if(ans>as){
				ans=as;
				q=i;
			}
		}
		update(1,1,N-5,a[i+1].st.st,-1);
		if(i>0)
			update(1,1,N-5,a[i-1].st.nd-a[i-1].st.st,1);
		sum+=M2[a[i].st.st];
	}

	cout<<ans<<endl;

	res[q]=2;

	for(i=1 ; i<q ; i++){
		s.insert(mp(a[i].st.nd-a[i].st.st,i));
		res[i]=1;
	}

	for(i=q+1 ; i<=n ; i++)
		s.insert(mp(a[i].st.st,i));

	int t=w-q;
	if(q)
		t--;

	for(i=1 ; i<=t ; i++){
		qwe=*s.begin();
		s.erase(s.begin());
		if(qwe.nd<q)
			res[qwe.nd]=2;
		else res[qwe.nd]=1;
	}

	for(i=1 ; i<=n ; i++)
		rr[a[i].nd]=res[i];

	for(i=1 ; i<=n ; i++)
		printf("%d",rr[i]);
}


#include<bits/stdc++.h>

using namespace std;

#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define sol (root+root)
#define sag (root+root+1)
#define orta ((bas+son)/2)
#define ll long long
#define pii pair<int,int>
#define int ll

const int N=3e5+5;
const int mod=1e9+7;
const int inf=1e9+7;

int n,w,i,ans=INT_MAX,sum,q,ss;
int res[N],rr[N],b[N];

pii st[N<<2],qwe;

pair<pii,int> a[N];

set<pii > s;

vector<int> v;

map<int,int> M,M2;

bool cmp(pair<pii,int> x,pair<pii,int> y){
	if(x.st.nd==y.st.nd)
		return x.st.st<y.st.st;
	return x.st.nd<y.st.nd;
}

int query(int root,int bas,int son,int q){

	if(bas == son)
		return min(M2[bas] * q, st[root].nd);

	if(st[sol].st>=q)
		return query(sol,bas,orta,q);

	return query(sag,orta+1,son,q-st[sol].st) + st[sol].nd;
}

int query2(int root,int bas,int son,int q){

	cout<<bas<<' '<<son<<' '<<q<<endl;

	if(bas == son)
		return min(M2[bas] * q, st[root].nd);

	if(st[sol].st>=q)
		return query2(sol,bas,orta,q);

	return query2(sag,orta+1,son,q-st[sol].st) + st[sol].nd;
}

pii update(int root,int bas,int son,int x,int v){
	if(bas>x or son<x)
		return st[root];

	if(bas==son){
		st[root].st+=v;
		st[root].nd+=v*(M2[bas]);
		return st[root];
	}

	pii t1=update(sol,bas,orta,x,v);
	pii t2=update(sag,orta+1,son,x,v);

	return st[root]=mp(t1.st+t2.st,t1.nd+t2.nd);
}

main(){

	scanf("%d %d",&n,&w);

	for(i=1 ; i<=n ; i++){
		scanf("%d %d",&a[i].st.st,&a[i].st.nd);
		a[i].nd=i;
		v.pb(a[i].st.st);
		v.pb(a[i].st.nd);
		v.pb(a[i].st.nd-a[i].st.st);
		b[i]=a[i].st.nd-a[i].st.st;
	}

	sort(v.begin(),v.end());

	v.resize(unique(v.begin(),v.end())-v.begin());

	for(i=0 ; i<v.size() ; i++){
		M[v[i]]=++ss;
		M2[ss]=v[i];
	}

	for(i=1 ; i<=n ; i++){
		a[i].st.st=M[a[i].st.st];
		a[i].st.nd=M[a[i].st.nd];
	}

	for(i=1 ; i<=n ; i++)
		b[i]=M[b[i]];

	sort(a+1,a+n+1,cmp);

	for(i=1 ; i<=n ; i++)
		update(1,1,N-5,a[i].st.st,1);

	for(i=0 ; i<=n ; i++){
		int t=w-i;
		if(i)
			t--;
		t=max(0,t);
		if(2*i+n-i>=w){
			int as=query(1,1,N-5,t)+M2[a[i].st.nd]+sum;
			if(ans>as){
				ans=as;
				q=i;
			}
		}
		update(1,1,N-5,a[i+1].st.st,-1);
		if(i>0)
			update(1,1,N-5,b[i],1);
		sum+=M2[a[i].st.st];
	}

	cout<<ans<<endl;

	res[q]=2;

	for(i=1 ; i<q ; i++){
		s.insert(mp(b[i],i));
		res[i]=1;
	}

	for(i=q+1 ; i<=n ; i++)
		s.insert(mp(a[i].st.st,i));

	int t=w-q;
	if(q)
		t--;

	for(i=1 ; i<=t ; i++){
		qwe=*s.begin();
		s.erase(s.begin());
		if(qwe.nd<q)
			res[qwe.nd]=2;
		else res[qwe.nd]=1;
	}

	for(i=1 ; i<=n ; i++)
		rr[a[i].nd]=res[i];

	for(i=1 ; i<=n ; i++)
		printf("%d",rr[i]);
}


#include<bits/stdc++.h>

using namespace std;

#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define sol (root+root)
#define sag (root+root+1)
#define orta ((bas+son)/2)
#define ll long long
#define pii pair<int,int>

const int N=3e5+5;
const int mod=1e9+7;
const int inf=1e9+7;

int n,w,i,ans=INT_MAX,sum,q,ss;
int res[N],rr[N],b[N];

pii st[N<<2],qwe;

pair<pii,int> a[N];

set<pii > s;

vector<int> v;

map<int,int> M,M2;

bool cmp(pair<pii,int> x,pair<pii,int> y){
	if(x.st.nd==y.st.nd)
		return x.st.st<y.st.st;
	return x.st.nd<y.st.nd;
}

int query(int root,int bas,int son,int q){

	if(bas == son)
		return min(M2[bas] * q, st[root].nd);

	if(st[sol].st>=q)
		return query(sol,bas,orta,q);

	return query(sag,orta+1,son,q-st[sol].st) + st[sol].nd;
}

int query2(int root,int bas,int son,int q){

	cout<<bas<<' '<<son<<' '<<q<<endl;

	if(bas == son)
		return min(M2[bas] * q, st[root].nd);

	if(st[sol].st>=q)
		return query2(sol,bas,orta,q);

	return query2(sag,orta+1,son,q-st[sol].st) + st[sol].nd;
}

pii update(int root,int bas,int son,int x,int v){
	if(bas>x or son<x)
		return st[root];

	if(bas==son){
		st[root].st+=v;
		st[root].nd+=v*(M2[bas]);
		return st[root];
	}

	pii t1=update(sol,bas,orta,x,v);
	pii t2=update(sag,orta+1,son,x,v);

	return st[root]=mp(t1.st+t2.st,t1.nd+t2.nd);
}

main(){

	scanf("%d %d",&n,&w);

	for(i=1 ; i<=n ; i++){
		scanf("%d %d",&a[i].st.st,&a[i].st.nd);
		a[i].nd=i;
		v.pb(a[i].st.st);
		v.pb(a[i].st.nd);
		v.pb(a[i].st.nd-a[i].st.st);
		b[i]=a[i].st.nd-a[i].st.st;
	}

	sort(v.begin(),v.end());

	v.resize(unique(v.begin(),v.end())-v.begin());

	for(i=0 ; i<v.size() ; i++){
		M[v[i]]=++ss;
		M2[ss]=v[i];
	}

	for(i=1 ; i<=n ; i++){
		a[i].st.st=M[a[i].st.st];
		a[i].st.nd=M[a[i].st.nd];
	}

	for(i=1 ; i<=n ; i++)
		b[i]=M[b[i]];

	sort(a+1,a+n+1,cmp);

	for(i=1 ; i<=n ; i++)
		update(1,1,N-5,a[i].st.st,1);

	for(i=0 ; i<=n ; i++){
		int t=w-i;
		if(i)
			t--;
		t=max(0,t);
		if(2*i+n-i>=w){
			int as=query(1,1,N-5,t)+M2[a[i].st.nd]+sum;
			if(ans>as){
				ans=as;
				q=i;
			}
		}
		update(1,1,N-5,a[i+1].st.st,-1);
		if(i>0)
			update(1,1,N-5,b[i],1);
		sum+=M2[a[i].st.st];
	}

	cout<<ans<<endl;

	res[q]=2;

	for(i=1 ; i<q ; i++){
		s.insert(mp(b[i],i));
		res[i]=1;
	}

	for(i=q+1 ; i<=n ; i++)
		s.insert(mp(a[i].st.st,i));

	int t=w-q;
	if(q)
		t--;

	for(i=1 ; i<=t ; i++){
		qwe=*s.begin();
		s.erase(s.begin());
		if(qwe.nd<q)
			res[qwe.nd]=2;
		else res[qwe.nd]=1;
	}

	for(i=1 ; i<=n ; i++)
		rr[a[i].nd]=res[i];

	for(i=1 ; i<=n ; i++)
		printf("%d",rr[i]);
}


#include<bits/stdc++.h>

using namespace std;

#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define sol (root+root)
#define sag (root+root+1)
#define orta ((bas+son)/2)
#define ll long long
#define pii pair<int,int>
#define int ll

const int N=3e5+5;
const int mod=1e9+7;
const int inf=1e9+7;

int n,w,i,ans=1e18,sum,q,ss;
int res[N],rr[N],b[N];

pii st[N<<2],qwe;

pair<pii,int> a[N];

set<pii > s;

vector<int> v;

map<int,int> M,M2;

bool cmp(pair<pii,int> x,pair<pii,int> y){
	if(x.st.nd==y.st.nd)
		return x.st.st<y.st.st;
	return x.st.nd<y.st.nd;
}

int query(int root,int bas,int son,int q){

	if(bas == son)
		return min(M2[bas] * q, st[root].nd);

	if(st[sol].st>=q)
		return query(sol,bas,orta,q);

	return query(sag,orta+1,son,q-st[sol].st) + st[sol].nd;
}

int query2(int root,int bas,int son,int q){

	cout<<bas<<' '<<son<<' '<<q<<endl;

	if(bas == son)
		return min(M2[bas] * q, st[root].nd);

	if(st[sol].st>=q)
		return query2(sol,bas,orta,q);

	return query2(sag,orta+1,son,q-st[sol].st) + st[sol].nd;
}

pii update(int root,int bas,int son,int x,int v){
	if(bas>x or son<x)
		return st[root];

	if(bas==son){
		st[root].st+=v;
		st[root].nd+=v*(M2[bas]);
		return st[root];
	}

	pii t1=update(sol,bas,orta,x,v);
	pii t2=update(sag,orta+1,son,x,v);

	return st[root]=mp(t1.st+t2.st,t1.nd+t2.nd);
}

main(){

	scanf("%d %d",&n,&w);

	for(i=1 ; i<=n ; i++){
		scanf("%d %d",&a[i].st.st,&a[i].st.nd);
		a[i].nd=i;
		v.pb(a[i].st.st);
		v.pb(a[i].st.nd);
		v.pb(a[i].st.nd-a[i].st.st);
		b[i]=a[i].st.nd-a[i].st.st;
	}

	sort(v.begin(),v.end());

	v.resize(unique(v.begin(),v.end())-v.begin());

	for(i=0 ; i<v.size() ; i++){
		M[v[i]]=++ss;
		M2[ss]=v[i];
	}

	for(i=1 ; i<=n ; i++){
		a[i].st.st=M[a[i].st.st];
		a[i].st.nd=M[a[i].st.nd];
	}

	for(i=1 ; i<=n ; i++)
		b[i]=M[b[i]];

	sort(a+1,a+n+1,cmp);

	for(i=1 ; i<=n ; i++)
		update(1,1,N-5,a[i].st.st,1);

	for(i=0 ; i<=n ; i++){
		int t=w-i;
		if(i)
			t--;
		t=max(0ll,t);
		if(2*i+n-i>=w){
			int as=query(1,1,N-5,t)+M2[a[i].st.nd]+sum;
			if(ans>as){
				ans=as;
				q=i;
			}
		}
		update(1,1,N-5,a[i+1].st.st,-1);
		if(i>0)
			update(1,1,N-5,b[i],1);
		sum+=M2[a[i].st.st];
	}

	cout<<ans<<endl;

	res[q]=2;

	for(i=1 ; i<q ; i++){
		s.insert(mp(b[i],i));
		res[i]=1;
	}

	for(i=q+1 ; i<=n ; i++)
		s.insert(mp(a[i].st.st,i));

	int t=w-q;
	if(q)
		t--;

	for(i=1 ; i<=t ; i++){
		qwe=*s.begin();
		s.erase(s.begin());
		if(qwe.nd<q)
			res[qwe.nd]=2;
		else res[qwe.nd]=1;
	}

	for(i=1 ; i<=n ; i++)
		rr[a[i].nd]=res[i];

	for(i=1 ; i<=n ; i++)
		printf("%d",rr[i]);
}


#include<bits/stdc++.h>

using namespace std;

#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define sol (root+root)
#define sag (root+root+1)
#define orta ((bas+son)/2)
#define ll long long
#define pii pair<int,int>
#define int ll

const int N=9e5+5;
const int mod=1e9+7;
const int inf=1e9+7;

int n,w,i,ans=1e18,sum,q,ss;
int res[N],rr[N],b[N];

pii st[N*4],qwe;

pair<pii,int> a[N];

set<pii > s;

vector<int> v;

map<int,int> M,M2;

bool cmp(pair<pii,int> x,pair<pii,int> y){
	if(x.st.nd==y.st.nd)
		return x.st.st<y.st.st;
	return x.st.nd<y.st.nd;
}

int query(int root,int bas,int son,int q){

	if(bas == son)
		return min(M2[bas] * q, st[root].nd);

	if(st[sol].st>=q)
		return query(sol,bas,orta,q);

	return query(sag,orta+1,son,q-st[sol].st) + st[sol].nd;
}

int query2(int root,int bas,int son,int q){

	cout<<bas<<' '<<son<<' '<<q<<endl;

	if(bas == son)
		return min(M2[bas] * q, st[root].nd);

	if(st[sol].st>=q)
		return query2(sol,bas,orta,q);

	return query2(sag,orta+1,son,q-st[sol].st) + st[sol].nd;
}

pii update(int root,int bas,int son,int x,int v){
	if(bas>x or son<x)
		return st[root];

	if(bas==son){
		st[root].st+=v;
		st[root].nd+=v*(M2[bas]);
		return st[root];
	}

	pii t1=update(sol,bas,orta,x,v);
	pii t2=update(sag,orta+1,son,x,v);

	return st[root]=mp(t1.st+t2.st,t1.nd+t2.nd);
}

main(){

	scanf("%lld %lld",&n,&w);

	for(i=1 ; i<=n ; i++){
		scanf("%lld %lld",&a[i].st.st,&a[i].st.nd);
		a[i].nd=i;
		v.pb(a[i].st.st);
		v.pb(a[i].st.nd);
		v.pb(a[i].st.nd-a[i].st.st);
		b[i]=a[i].st.nd-a[i].st.st;
	}

	sort(a+1,a+n+1,cmp);

	sort(v.begin(),v.end());

	v.resize(unique(v.begin(),v.end())-v.begin());

	for(i=0 ; i<v.size() ; i++){
		M[v[i]]=++ss;
		M2[ss]=v[i];
	}

	for(i=1 ; i<=n ; i++){
		a[i].st.st=M[a[i].st.st];
		a[i].st.nd=M[a[i].st.nd];
	}

	for(i=1 ; i<=n ; i++)
		b[i]=M[b[i]];

	for(i=1 ; i<=n ; i++)
		update(1,1,N-5,a[i].st.st,1);

	for(i=0 ; i<=n ; i++){
		int t=w-i;
		if(i)
			t--;
		t=max(0ll,t);
		if(2*i+n-i>=w and t>=0){
			int as=query(1,1,N-5,t)+M2[a[i].st.nd]+sum;
			if(ans>as){
				ans=as;
				q=i;
			}
		}
		update(1,1,N-5,a[i+1].st.st,-1);
		if(i)
			update(1,1,N-5,b[i],1);
		sum+=M2[a[i].st.st];
	}

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

	res[q]=2;

	for(i=1 ; i<q ; i++){
		s.insert(mp(b[i],i));
		res[i]=1;
	}

	for(i=q+1 ; i<=n ; i++)
		s.insert(mp(a[i].st.st,i));

	int t=w-q;
	if(q)
		t--;

	for(i=1 ; i<=t ; i++){
		qwe=*s.begin();
		s.erase(s.begin());
		if(qwe.nd<q)
			res[qwe.nd]=2;
		else res[qwe.nd]=1;
	}

	for(i=1 ; i<=n ; i++)
		rr[a[i].nd]=res[i];

	for(i=1 ; i<=n ; i++)
		printf("%lld",rr[i]);
}


#include<bits/stdc++.h>

using namespace std;

#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define sol (root+root)
#define sag (root+root+1)
#define orta ((bas+son)/2)
#define ll long long
#define pii pair<int,int>
#define int ll

const int N=9e5+5;
const int mod=1e9+7;
const int inf=1e9+7;

int n,w,i,ans=1e18,sum,q,ss;
int res[N],rr[N],b[N];

pii st[N*4],qwe;

pair<pii,int> a[N];

set<pii > s;

vector<int> v;

map<int,int> M,M2;

bool cmp(pair<pii,int> x,pair<pii,int> y){
	if(x.st.nd==y.st.nd)
		return x.st.st<y.st.st;
	return x.st.nd<y.st.nd;
}

int query(int root,int bas,int son,int q){

	if(bas == son)
		return min(M2[bas] * q, st[root].nd);

	if(st[sol].st>=q)
		return query(sol,bas,orta,q);

	return query(sag,orta+1,son,q-st[sol].st) + st[sol].nd;
}

int query2(int root,int bas,int son,int q){

	cout<<bas<<' '<<son<<' '<<q<<endl;

	if(bas == son)
		return min(M2[bas] * q, st[root].nd);

	if(st[sol].st>=q)
		return query2(sol,bas,orta,q);

	return query2(sag,orta+1,son,q-st[sol].st) + st[sol].nd;
}

pii update(int root,int bas,int son,int x,int v){
	if(bas>x or son<x)
		return st[root];

	if(bas==son){
		st[root].st+=v;
		st[root].nd+=v*(M2[bas]);
		return st[root];
	}

	pii t1=update(sol,bas,orta,x,v);
	pii t2=update(sag,orta+1,son,x,v);

	return st[root]=mp(t1.st+t2.st,t1.nd+t2.nd);
}

main(){

	scanf("%d %d",&n,&w);

	for(i=1 ; i<=n ; i++){
		scanf("%d %d",&a[i].st.st,&a[i].st.nd);
		a[i].nd=i;
		v.pb(a[i].st.st);
		v.pb(a[i].st.nd);
		v.pb(a[i].st.nd-a[i].st.st);
		b[i]=a[i].st.nd-a[i].st.st;
	}

	sort(v.begin(),v.end());

	v.resize(unique(v.begin(),v.end())-v.begin());

	for(i=0 ; i<v.size() ; i++){
		M[v[i]]=++ss;
		M2[ss]=v[i];
	}

	for(i=1 ; i<=n ; i++){
		a[i].st.st=M[a[i].st.st];
		a[i].st.nd=M[a[i].st.nd];
	}

	for(i=1 ; i<=n ; i++)
		b[i]=M[b[i]];

	sort(a+1,a+n+1,cmp);

	for(i=1 ; i<=n ; i++)
		update(1,1,N-5,a[i].st.st,1);

	for(i=0 ; i<=n ; i++){
		int t=w-i;
		if(i)
			t--;
		t=max(0ll,t);
		if(2*i+n-i>=w){
			int as=query(1,1,N-5,t)+M2[a[i].st.nd]+sum;
			if(ans>as){
				ans=as;
				q=i;
			}
		}
		update(1,1,N-5,a[i+1].st.st,-1);
		if(i>0)
			update(1,1,N-5,b[i],1);
		sum+=M2[a[i].st.st];
	}

	cout<<ans<<endl;

	res[q]=2;

	for(i=1 ; i<q ; i++){
		s.insert(mp(b[i],i));
		res[i]=1;
	}

	for(i=q+1 ; i<=n ; i++)
		s.insert(mp(a[i].st.st,i));

	int t=w-q;
	if(q)
		t--;

	for(i=1 ; i<=t ; i++){
		qwe=*s.begin();
		s.erase(s.begin());
		if(qwe.nd<q)
			res[qwe.nd]=2;
		else res[qwe.nd]=1;
	}

	for(i=1 ; i<=n ; i++)
		rr[a[i].nd]=res[i];

	for(i=1 ; i<=n ; i++)
		printf("%d",rr[i]);
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <queue>
#include <vector>

#define R "%d"
#define RL "%I64d"
#define RD "%lf"
#define W "%.8lf"

#define Max(x, y) ((x) > (y) ? (x) : (y))
#define Min(x, y) ((x) < (y) ? (x) : (y))
#define Swap(x, y) ((x) ^= (y), (y) ^= (x), (x) ^= (y))
#define Set(x, y) memset(x, y, sizeof(x))
#define Cpy(x, y) memcpy(x, y, sizeof(y))
#define All(x) x.begin(), x.end()
#define Sqr(x) ((x) * (x))
#define Sz(x) ((LL)x.size())

#define Mk make_pair
#define Pb push_back
#define Pk pop_back
#define Fr first
#define Se second

#define FOR(x, l, r) for (ll x = (l), end = (r); x <= end; ++x)
#define ROF(x, l, r) for (ll x = (l), end = (r); x >= end; --x)
#define Edge(x, y) for (LL x = g[y]; x; x = nt[x])

#define Read(x) ((x) = Get())
#define Lowbit(x) ((x) & (-x))
using namespace std;

typedef int LL;
typedef long long ll;
typedef double LD;
typedef long double real;

typedef map <LL, LL> MI;
typedef pair <LL, LL> PI;
typedef set <LL> SI;
typedef vector <LL> VI;

inline LL Get() {
	LL res = 0, q = 1;
	char ch = getchar();
	while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
	if (ch == '-') q = -1, ch = getchar();
	while (ch >= '0' && ch <= '9') res = res * 10 + ch - '0', ch = getchar();
	return res * q;
}

const LD eps = 1e-12, pi = M_PI;
const LL oo = (LL)2e9, mod = (LL)1e9 + 7;
const ll INF = (ll)1e17;

const LL N = (LL)3e5;
typedef ll arr[N + 10];

struct node {
	ll x, y, z;
}a[N + 10], b[N + 10];

ll i, j, k, p, n, ma, mb, w, ans = INF, cal, mx, mxn, s, prv, prls, prmxn;
arr pr, mi, mip, sa;

bool cmp_a(const node &a, const node &b) { return a.x < b.x; }

bool cmp_b(const node &a, const node &b) { return a.x + a.y < b.x + b.y; }

LL main() {
#ifndef ONLINE_JUDGE
	freopen("card.in", "r", stdin);
	freopen("card.out", "w", stdout);
#endif

	scanf(RL RL "\n", &n, &w);
	FOR (i, 1, n) {
		scanf(RL RL "\n", &j, &k);
		if (k > 2 * j) a[++ma] = (node){j, i, 1}, a[++ma] = (node){k - j, i, 2};
		else b[++mb] = (node){j, k - j, i};
	}
	sort(a + 1, a + ma + 1, cmp_a), sort(b + 1, b + mb + 1, cmp_b);

	mi[mb + 1] = oo;
	ROF (i, mb, 1)
		if (mi[i + 1] < b[i].x) mi[i] = mi[i + 1], mip[i] = mip[i + 1];
		else mi[i] = b[i].x, mip[i] = i;

	FOR (i, 1, ma) sa[i] = sa[i - 1] + a[i].x;

	FOR (ls, 0, Min(w, mb << 1)) {
		if (ls & 1) {
			p = (ls + 1) >> 1, s += b[p].x + b[p].y;
			if (b[p].y > mx) mx = b[p].y, mxn = p;
			if (w - ls > ma) continue;
			ll go = -1, ck = 0;;
			if (s - mx < s - b[p].x - b[p].y + mi[p + 1]) go = 0, ck = s - mx + sa[w - ls];
			else go = 1, ck = s - b[p].x - b[p].y + mi[p + 1] + sa[w - ls];
			if (ck < ans) {
				ans = ck;
				if (go == 0) {
					prv = 0, prls = ls, prmxn = mxn;
				}
				else {
					prv = 1, prls = ls;
				}
			}
		}
		else {
			p = ls >> 1;
			if (w - ls > ma) continue;
			ll ck = s + sa[w - ls];
			if (ck < ans) {
				ans = ck;
				prv = 2, prls = ls;
			}
		}
	}

	printf(RL "\n", ans);

	p = ((prls & 1) ? (prls + 1) >> 1 : prls >> 1);
	if (prv == 0) {
		FOR (i, 1, p)
			if (i != prmxn) pr[b[i].z] = 2;
			else pr[b[i].z] = 1;
		FOR (i, 1, w - prls) pr[a[i].y] = Max(pr[a[i].y], a[i].z);
	}
	else if (prv == 1) {
		FOR (i, 1, p - 1) pr[b[i].z] = 2;
		pr[b[mip[p + 1]].z] = 1;
		FOR (i, 1, w - prls) pr[a[i].y] = Max(pr[a[i].y], a[i].z);
	}
	else {
		FOR (i, 1, p) pr[b[i].z] = 2;
		FOR (i, 1, w - prls) pr[a[i].y] = Max(pr[a[i].y], a[i].z);
	}

	FOR (i, 1, n) printf(RL, pr[i]);

	return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <queue>
#include <vector>

#define R "%d"
#define RL "%I64d"
#define RD "%lf"
#define W "%.8lf"

#define Max(x, y) ((x) > (y) ? (x) : (y))
#define Min(x, y) ((x) < (y) ? (x) : (y))
#define Swap(x, y) ((x) ^= (y), (y) ^= (x), (x) ^= (y))
#define Set(x, y) memset(x, y, sizeof(x))
#define Cpy(x, y) memcpy(x, y, sizeof(y))
#define All(x) x.begin(), x.end()
#define Sqr(x) ((x) * (x))
#define Sz(x) ((LL)x.size())

#define Mk make_pair
#define Pb push_back
#define Pk pop_back
#define Fr first
#define Se second

#define FOR(x, l, r) for (ll x = (l), end = (r); x <= end; ++x)
#define ROF(x, l, r) for (ll x = (l), end = (r); x >= end; --x)
#define Edge(x, y) for (LL x = g[y]; x; x = nt[x])

#define Read(x) ((x) = Get())
#define Lowbit(x) ((x) & (-x))
using namespace std;

typedef int LL;
typedef long long ll;
typedef double LD;
typedef long double real;

typedef map <LL, LL> MI;
typedef pair <LL, LL> PI;
typedef set <LL> SI;
typedef vector <LL> VI;

inline LL Get() {
	LL res = 0, q = 1;
	char ch = getchar();
	while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
	if (ch == '-') q = -1, ch = getchar();
	while (ch >= '0' && ch <= '9') res = res * 10 + ch - '0', ch = getchar();
	return res * q;
}

const LD eps = 1e-12, pi = M_PI;
const LL oo = (LL)2e9, mod = (LL)1e9 + 7;
const ll INF = (ll)1e17;

const LL N = (LL)6e5;
typedef ll arr[N + 10];

struct node {
	ll x, y, z;
}a[N + 10], b[N + 10];

ll i, j, k, p, n, ma, mb, w, ans = INF, cal, mx, mxn, s, prv, prls, prmxn;
arr pr, mi, mip, sa;

bool cmp_a(const node &a, const node &b) { return a.x < b.x; }

bool cmp_b(const node &a, const node &b) { return a.x + a.y < b.x + b.y; }

LL main() {
#ifndef ONLINE_JUDGE
	freopen("card.in", "r", stdin);
	freopen("card.out", "w", stdout);
#endif

	scanf(RL RL "\n", &n, &w);
	FOR (i, 1, n) {
		scanf(RL RL "\n", &j, &k);
		if (k > 2 * j) a[++ma] = (node){j, i, 1}, a[++ma] = (node){k - j, i, 2};
		else b[++mb] = (node){j, k - j, i};
	}
	sort(a + 1, a + ma + 1, cmp_a), sort(b + 1, b + mb + 1, cmp_b);

	mi[mb + 1] = oo;
	ROF (i, mb, 1)
		if (mi[i + 1] < b[i].x) mi[i] = mi[i + 1], mip[i] = mip[i + 1];
		else mi[i] = b[i].x, mip[i] = i;

	FOR (i, 1, ma) sa[i] = sa[i - 1] + a[i].x;

	FOR (ls, 0, Min(w, mb << 1)) {
		if (ls & 1) {
			p = (ls + 1) >> 1, s += b[p].x + b[p].y;
			if (b[p].y > mx) mx = b[p].y, mxn = p;
			if (w - ls > ma) continue;
			ll go = -1, ck = 0;;
			if (s - mx < s - b[p].x - b[p].y + mi[p + 1]) go = 0, ck = s - mx + sa[w - ls];
			else go = 1, ck = s - b[p].x - b[p].y + mi[p + 1] + sa[w - ls];
			if (ck < ans) {
				ans = ck;
				if (go == 0) {
					prv = 0, prls = ls, prmxn = mxn;
				}
				else {
					prv = 1, prls = ls;
				}
			}
		}
		else {
			p = ls >> 1;
			if (w - ls > ma) continue;
			ll ck = s + sa[w - ls];
			if (ck < ans) {
				ans = ck;
				prv = 2, prls = ls;
			}
		}
	}

	printf(RL "\n", ans);

	p = ((prls & 1) ? (prls + 1) >> 1 : prls >> 1);
	if (prv == 0) {
		FOR (i, 1, p)
			if (i != prmxn) pr[b[i].z] = 2;
			else pr[b[i].z] = 1;
		FOR (i, 1, w - prls) pr[a[i].y] = Max(pr[a[i].y], a[i].z);
	}
	else if (prv == 1) {
		FOR (i, 1, p - 1) pr[b[i].z] = 2;
		pr[b[mip[p + 1]].z] = 1;
		FOR (i, 1, w - prls) pr[a[i].y] = Max(pr[a[i].y], a[i].z);
	}
	else {
		FOR (i, 1, p) pr[b[i].z] = 2;
		FOR (i, 1, w - prls) pr[a[i].y] = Max(pr[a[i].y], a[i].z);
	}

	FOR (i, 1, n) printf(RL, pr[i]);

	return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <queue>
#include <vector>

#define R "%d"
#define RL "%I64d"
#define RD "%lf"
#define W "%.8lf"

#define Max(x, y) ((x) > (y) ? (x) : (y))
#define Min(x, y) ((x) < (y) ? (x) : (y))
#define Swap(x, y) ((x) ^= (y), (y) ^= (x), (x) ^= (y))
#define Set(x, y) memset(x, y, sizeof(x))
#define Cpy(x, y) memcpy(x, y, sizeof(y))
#define All(x) x.begin(), x.end()
#define Sqr(x) ((x) * (x))
#define Sz(x) ((LL)x.size())

#define Mk make_pair
#define Pb push_back
#define Pk pop_back
#define Fr first
#define Se second

#define FOR(x, l, r) for (ll x = (l), end = (r); x <= end; ++x)
#define ROF(x, l, r) for (ll x = (l), end = (r); x >= end; --x)
#define Edge(x, y) for (LL x = g[y]; x; x = nt[x])

#define Read(x) ((x) = Get())
#define Lowbit(x) ((x) & (-x))
using namespace std;

typedef int LL;
typedef long long ll;
typedef double LD;
typedef long double real;

typedef map <LL, LL> MI;
typedef pair <LL, LL> PI;
typedef set <LL> SI;
typedef vector <LL> VI;

inline LL Get() {
	LL res = 0, q = 1;
	char ch = getchar();
	while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
	if (ch == '-') q = -1, ch = getchar();
	while (ch >= '0' && ch <= '9') res = res * 10 + ch - '0', ch = getchar();
	return res * q;
}

const LD eps = 1e-12, pi = M_PI;
const LL oo = (LL)2e9, mod = (LL)1e9 + 7;
const ll INF = (ll)1e17;

const LL N = (LL)3e5;
typedef ll arr[N + 10];

struct node {
	ll x, y, z;
}a[N + 10], b[N + 10];

ll i, j, k, p, n, ma, mb, w, ans = INF, cal, mx, mxn, s;
arr pr, mi, mip, sa;

bool cmp_a(const node &a, const node &b) { return a.x < b.x; }

bool cmp_b(const node &a, const node &b) { return a.x + a.y < b.x + b.y; }

LL main() {
#ifndef ONLINE_JUDGE
	freopen("card.in", "r", stdin);
	freopen("card.out", "w", stdout);
#endif

	scanf(RL RL "\n", &n, &w);
	FOR (i, 1, n) {
		scanf(RL RL "\n", &j, &k);
		if (k > 2 * j) a[++ma] = (node){j, i, 1}, a[++ma] = (node){k - j, i, 2};
		else b[++mb] = (node){j, k - j, i};
	}
	sort(a + 1, a + ma + 1, cmp_a), sort(b + 1, b + mb + 1, cmp_b);

	mi[mb + 1] = oo;
	ROF (i, mb, 1)
		if (mi[i + 1] < b[i].x) mi[i] = mi[i + 1], mip[i] = mip[i + 1];
		else mi[i] = b[i].x, mip[i] = i;

	FOR (i, 1, ma) sa[i] = sa[i - 1] + a[i].x;

	FOR (ls, 0, Min(w, mb << 1)) {
		if (ls & 1) {
			p = (ls + 1) >> 1, s += b[p].x + b[p].y;
			if (b[p].y > mx) mx = b[p].y, mxn = p;
			if (w - ls > ma) continue;
			ll go = -1, ck = 0;;
			if (s - mx < s - b[p].x - b[p].y + mi[p + 1]) go = 0, ck = s - mx + sa[w - ls];
			else go = 1, ck = s - b[p].x - b[p].y + mi[p + 1] + sa[w - ls];
			if (ck < ans) {
				ans = ck;
				memset(pr, 0, sizeof(pr));
				if (go == 0) {
					FOR (i, 1, p)
						if (i != mxn) pr[b[i].z] = 2;
						else pr[b[i].z] = 1;
					FOR (i, 1, w - ls) pr[a[i].y] = Max(pr[a[i].y], a[i].z);
				}
				else {
					FOR (i, 1, p - 1) pr[b[i].z] = 2;
					pr[b[mip[p + 1]].z] = 1;
					FOR (i, 1, w - ls) pr[a[i].y] = Max(pr[a[i].y], a[i].z);
				}
			}
		}
		else {
			p = ls >> 1;
			if (w - ls > ma) continue;
			ll ck = s + sa[w - ls];
			if (ck < ans) {
				ans = ck;
				memset(pr, 0, sizeof(pr));
				FOR (i, 1, p) pr[b[i].z] = 2;
				FOR (i, 1, w - ls) pr[a[i].y] = Max(pr[a[i].y], a[i].z);
			}
		}
	}

	printf(RL "\n", ans);
	FOR (i, 1, n) printf(RL, pr[i]);

	return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <queue>
#include <vector>

#define R "%d"
#define RL "%I64d"
#define RD "%lf"
#define W "%.8lf"

#define Max(x, y) ((x) > (y) ? (x) : (y))
#define Min(x, y) ((x) < (y) ? (x) : (y))
#define Swap(x, y) ((x) ^= (y), (y) ^= (x), (x) ^= (y))
#define Set(x, y) memset(x, y, sizeof(x))
#define Cpy(x, y) memcpy(x, y, sizeof(y))
#define All(x) x.begin(), x.end()
#define Sqr(x) ((x) * (x))
#define Sz(x) ((LL)x.size())

#define Mk make_pair
#define Pb push_back
#define Pk pop_back
#define Fr first
#define Se second

#define FOR(x, l, r) for (ll x = (l), end = (r); x <= end; ++x)
#define ROF(x, l, r) for (ll x = (l), end = (r); x >= end; --x)
#define Edge(x, y) for (LL x = g[y]; x; x = nt[x])

#define Read(x) ((x) = Get())
#define Lowbit(x) ((x) & (-x))
using namespace std;

typedef int LL;
typedef long long ll;
typedef double LD;
typedef long double real;

typedef map <LL, LL> MI;
typedef pair <LL, LL> PI;
typedef set <LL> SI;
typedef vector <LL> VI;

inline LL Get() {
	LL res = 0, q = 1;
	char ch = getchar();
	while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
	if (ch == '-') q = -1, ch = getchar();
	while (ch >= '0' && ch <= '9') res = res * 10 + ch - '0', ch = getchar();
	return res * q;
}

const LD eps = 1e-12, pi = M_PI;
const LL oo = (LL)2e9, mod = (LL)1e9 + 7;
const ll INF = (ll)1e17;

const LL N = (LL)3e5;
typedef ll arr[N + 10];

struct node {
	ll x, y, z;
}a[N + 10], b[N + 10];

ll i, j, k, p, n, ma, mb, w, ans = oo, cal, mx, mxn, s;
arr pr, mi, mip, sa;

bool cmp_a(const node &a, const node &b) { return a.x < b.x; }

bool cmp_b(const node &a, const node &b) { return a.x + a.y < b.x + b.y; }

LL main() {
#ifndef ONLINE_JUDGE
	freopen("card.in", "r", stdin);
	freopen("card.out", "w", stdout);
#endif

	scanf(RL RL "\n", &n, &w);
	FOR (i, 1, n) {
		scanf(RL RL "\n", &j, &k);
		if (k > 2 * j) a[++ma] = (node){j, i, 1}, a[++ma] = (node){k - j, i, 2};
		else b[++mb] = (node){j, k - j, i};
	}
	sort(a + 1, a + ma + 1, cmp_a), sort(b + 1, b + mb + 1, cmp_b);

	mi[mb + 1] = oo;
	ROF (i, mb, 1)
		if (mi[i + 1] < b[i].x) mi[i] = mi[i + 1], mip[i] = mip[i + 1];
		else mi[i] = b[i].x, mip[i] = i;

	FOR (i, 1, ma) sa[i] = sa[i - 1] + a[i].x;

	FOR (ls, 0, Min(w, mb << 1)) {
		if (ls & 1) {
			p = (ls + 1) >> 1, s += b[p].x + b[p].y;
			if (b[p].y > mx) mx = b[p].y, mxn = p;
			if (w - ls > ma) continue;
			ll go = -1, ck = 0;;
			if (s - mx < s - b[p].x - b[p].y + mi[p + 1]) go = 0, ck = s - mx + sa[w - ls];
			else go = 1, ck = s - b[p].x - b[p].y + mi[p + 1] + sa[w - ls];
			if (ck < ans) {
				ans = ck;
				memset(pr, 0, sizeof(pr));
				if (go == 0) {
					FOR (i, 1, p)
						if (i != mxn) pr[b[i].z] = 2;
						else pr[b[i].z] = 1;
					FOR (i, 1, w - ls) pr[a[i].y] = Max(pr[a[i].y], a[i].z);
				}
				else {
					FOR (i, 1, p - 1) pr[b[i].z] = 2;
					pr[b[mip[p + 1]].z] = 1;
					FOR (i, 1, w - ls) pr[a[i].y] = Max(pr[a[i].y], a[i].z);
				}
			}
		}
		else {
			p = ls >> 1;
			if (w - ls > ma) continue;
			ll ck = s + sa[w - ls];
			if (ck < ans) {
				ans = ck;
				memset(pr, 0, sizeof(pr));
				FOR (i, 1, p) pr[b[i].z] = 2;
				FOR (i, 1, w - ls) pr[a[i].y] = Max(pr[a[i].y], a[i].z);
			}
		}
	}

	printf(RL "\n", ans);
	FOR (i, 1, n) printf(RL, pr[i]);

	return 0;
}


#include <vector>
#include <stdio.h>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;

vi res, v1, v2;
ll sum;

set<ii> s1;
set<ii> s2;

int main()
{
    ll n, m;

    scanf("%lld %lld", &n, &m);
    res.resize(n, 0);
    v1.resize(n);
    v2.resize(n);
    sum = 0;

    for (int i = 0; i < n; i++)
    {
        scanf("%d %d", &v1[i], &v2[i]);
        s1.insert(ii(v1[i], i));
        s2.insert(ii(v2[i], i));
    }

    for (int i = 0; i < m; i++)
    {
        if (i < m - 1 && !s2.empty() && s1.size() > 1)
        {
            ii p1 = *s1.begin();
            s1.erase(s1.begin());
            ii p2 = *s1.begin();
            s1.erase(s1.begin());
            ii p = *s2.begin();
            if (p.first < p1.first + p2.first)
            {
                s2.erase(s2.begin());

                // Estrellas no conseguidas regresan
                s1.insert(p1);
                s1.insert(p2);

                // Actualizar estrellas conseguidas
                sum += v1[p.second];
                res[p.second]++;
                s1.erase(ii(v1[p.second], p.second));
                s1.insert(ii(v2[p.second] - v1[p.second], p.second));
                continue;
            }

            s1.insert(p1);
            s1.insert(p2);
        }

        ii p = *s1.begin();
        s1.erase(s1.begin());
        sum += p.first;
        res[p.second]++;
        if (res[p.second] == 1)
        {
            s2.erase(ii(v2[p.second], p.second));
            s1.insert(ii(v2[p.second] - v1[p.second], p.second));
        }
    }

    printf("%lld\n", sum);
    for (int i = 0; i < n; i++)
        printf("%d", res[i]);
    printf("\n");
}


#include <vector>
#include <stdio.h>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;

vi res, v1, v2;
ll sum;

set<ii> s1;
set<ii> s2;

int main()
{
    ll n, m;

    scanf("%lld %lld", &n, &m);
    res.resize(n, 0);
    v1.resize(n);
    v2.resize(n);
    sum = 0;

    for (int i = 0; i < n; i++)
    {
        scanf("%d %d", &v1[i], &v2[i]);
        s1.insert(ii(v1[i], i));
        s2.insert(ii(v2[i], i));
    }

    while (m > 0)
    {
        m--;
        if (m > 2 && !s2.empty() && s1.size() > 1)
        {
            ii p1 = *s1.begin();
            s1.erase(s1.begin());
            ii p2 = *s1.begin();
            s1.erase(s1.begin());
            ii p = *s2.begin();
            if (p.first < p1.first + p2.first)
            {
                s2.erase(s2.begin());

                // Estrellas no conseguidas regresan
                s1.insert(p1);
                s1.insert(p2);

                // Actualizar estrellas conseguidas
                sum += v1[p.second];
                res[p.second]++;
                s1.erase(ii(v1[p.second], p.second));
                s1.insert(ii(v2[p.second] - v1[p.second], p.second));
                continue;
            }

            s1.insert(p1);
            s1.insert(p2);
        }

        ii p = *s1.begin();
        s1.erase(s1.begin());
        sum += p.first;
        res[p.second]++;
        if (res[p.second] == 1)
        {
            s2.erase(ii(v2[p.second], p.second));
            s1.insert(ii(v2[p.second] - v1[p.second], p.second));
        }
    }

    printf("%lld\n", sum);
    for (int i = 0; i < n; i++)
        printf("%d", res[i]);
    printf("\n");
}


#include<bits/stdc++.h>
#include<cstdio>

#define deb(x) cout << #x << " -> " << x << el
#define db double
#define ll long long int
#define ld long double
#define ull unsigned long long int
#define sc(x) scanf("%d", &x)
#define scl(x) scanf("%lld", &x)
#define sclu(x) scanf("%llu", &x)
#define scd(x) scanf("%lf", &x)
#define scld(x) scanf("%Lf", &x)
#define pass cout<<"pass\n"
#define vi vector<int>
#define eb emplace_back
#define pb push_back
#define vec vector
#define el "\n"
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define f(i,x,y) for(int i=x;i<y;++i)
#define ff(i,x,y) for(int i=x;i<=y;++i)
#define mem(x) memset(x, 0, sizeof(x))
#define mems(x,y) memset(x, y, sizeof(x))
#define MOD 998244353
#define MAX 200005

using namespace std;

int n, k, x;
pii val[MAX];

void solve() {
	sc(n); sc(k);
	for(int i = 0; i < n; ++i) {
		sc(x);
		val[i] = { x, i };
	}

	sort(val, val + n, greater<pii>());
	sort(val, val + k, [](pii a, pii b) -> bool {
		return a.S < b.S;
	});

	ll ans = 1LL, sum = 1LL * val[0].F;
	for(int i = 1; i < k; ++i) {
		sum += val[i].F;
		ll gap = val[i].S - val[i - 1].S;
		ans = (1LL * ans * gap) % MOD;
	}
	cout << sum << " " << ans << el;
}

int main() {
	// clock_t start = clock();
	// freopen("./input.in", "r", stdin);
	int tt = 1;
	// sc(tt);
	while(tt--) {
		solve();
	}
	// clock_t finish = clock();
	// printf("Time taken: %0.9lf sec\n", (double)((finish - start)) / CLOCKS_PER_SEC);

	return 0;
}


#include<bits/stdc++.h>
#include<cstdio>

#define deb(x) cout << #x << " -> " << x << el
#define db double
#define ll long long int
#define ld long double
#define ull unsigned long long int
#define sc(x) scanf("%d", &x)
#define scl(x) scanf("%lld", &x)
#define sclu(x) scanf("%llu", &x)
#define scd(x) scanf("%lf", &x)
#define scld(x) scanf("%Lf", &x)
#define pass cout<<"pass\n"
#define vi vector<int>
#define eb emplace_back
#define pb push_back
#define vec vector
#define el "\n"
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define f(i,x,y) for(int i=x;i<y;++i)
#define ff(i,x,y) for(int i=x;i<=y;++i)
#define mem(x) memset(x, 0, sizeof(x))
#define mems(x,y) memset(x, y, sizeof(x))
#define MOD 998244353
#define MAX 200005

using namespace std;

int n, k, x;
pii val[MAX];

void solve() {
	sc(n); sc(k);
	ll sum = 0LL, freq = 1LL;
	int prev = -1;
	for(int i = 0; i < n; ++i) {
		sc(x);
		if(x > n - k) {
			sum = (sum + x) % MOD;
			if(prev != -1) {
				freq = (1LL * freq * (i - prev)) % MOD;
			}
			prev = i;
		}
	}
	cout << sum << " " << freq << el;
}

int main() {
	// clock_t start = clock();
	// freopen("./input.in", "r", stdin);
	int tt = 1;
	// sc(tt);
	while(tt--) {
		solve();
	}
	// clock_t finish = clock();
	// printf("Time taken: %0.9lf sec\n", (double)((finish - start)) / CLOCKS_PER_SEC);

	return 0;
}


#include<bits/stdc++.h>
#include<cstdio>

#define deb(x) cout << #x << " -> " << x << el
#define db double
#define ll long long int
#define ld long double
#define ull unsigned long long int
#define sc(x) scanf("%d", &x)
#define scl(x) scanf("%lld", &x)
#define sclu(x) scanf("%llu", &x)
#define scd(x) scanf("%lf", &x)
#define scld(x) scanf("%Lf", &x)
#define pass cout<<"pass\n"
#define vi vector<int>
#define eb emplace_back
#define pb push_back
#define vec vector
#define el "\n"
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define f(i,x,y) for(int i=x;i<y;++i)
#define ff(i,x,y) for(int i=x;i<=y;++i)
#define mem(x) memset(x, 0, sizeof(x))
#define mems(x,y) memset(x, y, sizeof(x))
#define MOD 998244353
#define MAX 200005

using namespace std;

int n, k, x;
pii val[MAX];

void solve() {
	sc(n); sc(k);
	for(int i = 0; i < n; ++i) {
		sc(x);
		val[i] = { x, i };
	}

	sort(val, val + n, greater<pii>());
	sort(val, val + k, [](pii a, pii b) -> bool {
		return a.S < b.S;
	});

	ll ans = 1LL, sum = 1LL * val[0].F;
	for(int i = 1; i < k; ++i) {
		sum = (0LL + sum + val[i].F) % MOD;
		ll gap = val[i].S - val[i - 1].S;
		ans = (1LL * ans * gap) % MOD;
	}
	cout << sum << " " << ans << el;
}

int main() {
	// clock_t start = clock();
	// freopen("./input.in", "r", stdin);
	int tt = 1;
	// sc(tt);
	while(tt--) {
		solve();
	}
	// clock_t finish = clock();
	// printf("Time taken: %0.9lf sec\n", (double)((finish - start)) / CLOCKS_PER_SEC);

	return 0;
}


#include<bits/stdc++.h>
#include<cstdio>

#define deb(x) cout << #x << " -> " << x << el
#define db double
#define ll long long int
#define ld long double
#define ull unsigned long long int
#define sc(x) scanf("%d", &x)
#define scl(x) scanf("%lld", &x)
#define sclu(x) scanf("%llu", &x)
#define scd(x) scanf("%lf", &x)
#define scld(x) scanf("%Lf", &x)
#define pass cout<<"pass\n"
#define vi vector<int>
#define eb emplace_back
#define pb push_back
#define vec vector
#define el "\n"
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define f(i,x,y) for(int i=x;i<y;++i)
#define ff(i,x,y) for(int i=x;i<=y;++i)
#define mem(x) memset(x, 0, sizeof(x))
#define mems(x,y) memset(x, y, sizeof(x))
#define MOD 998244353
#define MAX 200005

using namespace std;

int n, k, x;
pii val[MAX];

void solve() {
	sc(n); sc(k);
	for(int i = 0; i < n; ++i) {
		sc(x);
		val[i] = { x, i };
	}

	sort(val, val + n, greater<pii>());
	sort(val, val + k, [](pii a, pii b) -> bool {
		return a.S < b.S;
	});

	ll ans = 1LL, sum = 1LL * val[0].F;
	for(int i = 1; i < k; ++i) {
		sum = (sum + val[i].F) % MOD;
		ll gap = val[i].S - val[i - 1].S;
		ans = (ans * gap) % MOD;
	}
	cout << sum << " " << ans << el;
}

int main() {
	// clock_t start = clock();
	// freopen("./input.in", "r", stdin);
	int tt = 1;
	// sc(tt);
	while(tt--) {
		solve();
	}
	// clock_t finish = clock();
	// printf("Time taken: %0.9lf sec\n", (double)((finish - start)) / CLOCKS_PER_SEC);

	return 0;
}


#include<bits/stdc++.h>
#include<cstdio>

#define deb(x) cout << #x << " -> " << x << el
#define db double
#define ll long long int
#define ld long double
#define ull unsigned long long int
#define sc(x) scanf("%d", &x)
#define scl(x) scanf("%lld", &x)
#define sclu(x) scanf("%llu", &x)
#define scd(x) scanf("%lf", &x)
#define scld(x) scanf("%Lf", &x)
#define pass cout<<"pass\n"
#define vi vector<int>
#define eb emplace_back
#define pb push_back
#define vec vector
#define el "\n"
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define f(i,x,y) for(int i=x;i<y;++i)
#define ff(i,x,y) for(int i=x;i<=y;++i)
#define mem(x) memset(x, 0, sizeof(x))
#define mems(x,y) memset(x, y, sizeof(x))
#define MOD 998244353
#define MAX 200005

using namespace std;

int n, k, x;
pii val[MAX];

void solve() {
	sc(n); sc(k);
	ll sum = 0LL, freq = 1LL;
	int prev = -1;
	for(int i = 0; i < n; ++i) {
		sc(x);
		if(x > n - k) {
			sum += x;
			if(prev != -1) {
				freq = (1LL * freq * (i - prev)) % MOD;
			}
			prev = i;
		}
	}
	cout << sum << " " << freq << el;
}

int main() {
	// clock_t start = clock();
	// freopen("./input.in", "r", stdin);
	int tt = 1;
	// sc(tt);
	while(tt--) {
		solve();
	}
	// clock_t finish = clock();
	// printf("Time taken: %0.9lf sec\n", (double)((finish - start)) / CLOCKS_PER_SEC);

	return 0;
}


#include<bits/stdc++.h>
#include<cstdio>

#define deb(x) cout << #x << " -> " << x << el
#define db double
#define ll long long int
#define ld long double
#define ull unsigned long long int
#define sc(x) scanf("%d", &x)
#define scl(x) scanf("%lld", &x)
#define sclu(x) scanf("%llu", &x)
#define scd(x) scanf("%lf", &x)
#define scld(x) scanf("%Lf", &x)
#define pass cout<<"pass\n"
#define vi vector<int>
#define eb emplace_back
#define pb push_back
#define vec vector
#define el "\n"
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define f(i,x,y) for(int i=x;i<y;++i)
#define ff(i,x,y) for(int i=x;i<=y;++i)
#define mem(x) memset(x, 0, sizeof(x))
#define mems(x,y) memset(x, y, sizeof(x))
#define MOD 998244353
#define MAX 200005

using namespace std;

int n, k, x;
pii val[MAX];

void solve() {
	sc(n); sc(k);
	ll sum = 0LL, freq = 1LL;
	int prev = -1;
	for(int i = 0; i < n; ++i) {
		sc(x);
		if(x > n - k) {
			sum = (sum + x) % MOD;
			if(prev != -1) {
				freq = (freq * (i - prev)) % MOD;
			}
			prev = i;
		}
	}
	cout << sum << " " << freq << el;
}

int main() {
	// clock_t start = clock();
	// freopen("./input.in", "r", stdin);
	int tt = 1;
	// sc(tt);
	while(tt--) {
		solve();
	}
	// clock_t finish = clock();
	// printf("Time taken: %0.9lf sec\n", (double)((finish - start)) / CLOCKS_PER_SEC);

	return 0;
}