#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <utility>
#include <list>
#include <queue>
#include <deque>
#include <iterator>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>

#define MAX_INT 2147483647
#define MAX_LONG 9223372036854775807
#define MAX_ULONG 18446744073709551615

using namespace std;

priority_queue<pair<int, int> > kayacs, catamarans;
vector<int> res;

int main () {

	int n, v, t, p, i, j, tot, kn, cn, klim;

	cin>>n>>v;

	for (i=1; i<=n; i++) {
		cin>>t>>p;
		if (t == 1) kayacs.push(make_pair(p, i));
		else catamarans.push(make_pair(p, i));
	}

	pair<int, int> k1, k2, c;
	tot = 0;
	kn = kayacs.size();
	cn = catamarans.size();
	if (v % 2 == 1 && kn) {
		tot += kayacs.top().first;
		res.push_back(kayacs.top().second);
		kayacs.pop();
		kn--;
		v--;
	}
	if (!kayacs.empty()) {
		k1 = kayacs.top();
		kayacs.pop();
	}
	if (!kayacs.empty()) {
		k2 = kayacs.top();
		kayacs.pop();
	}
	if (cn) {
		c = catamarans.top();
		catamarans.pop();
	}

	while (v > 1 && kn > 1 && cn > 0) {

		if (k1.first + k2.first > c.first) {
			tot = tot + k1.first + k2.first;
			res.push_back(k1.second);
			res.push_back(k2.second);
			kn -= 2;
			v -= 2;

			if (kn > 0) {
				k1 = kayacs.top();
				kayacs.pop();

				if (kn > 1) {
					k2 = kayacs.top();
					kayacs.pop();
				}
			}
		} else {
			tot += c.first;
			res.push_back(c.second);
			cn--;
			v -= 2;

			if (cn > 0) {
				c = catamarans.top();
				catamarans.pop();
			}
		}
	}

	if (v != 0) {

		if (cn == 0) {
			if (kn >= 2) kayacs.push(k2);
			while (kn && v) {
				tot += k1.first;
				res.push_back(k1.second);
				kn--;
				v--;

				if (kn) {
					k1 = kayacs.top();
					kayacs.pop();
				}
			}
		} else {

			if (kn == 0) {
				while (cn && v > 1) {
					tot += c.first;
					res.push_back(c.second);
					cn--;
					v -= 2;

					if (cn > 0) {
						c = catamarans.top();
						catamarans.pop();
					}
				}
			} else {

				if (kn == 1) {
					while (kn && cn && v > 1) {
						if (k1.first > c.first) {
							tot += k1.first;
							res.push_back(k1.second);
							kn--;
							v--;
						} else {
							tot += c.first;
							res.push_back(c.second);
							cn--;
							v -= 2;

							if (cn > 0) {
								c = catamarans.top();
								catamarans.pop();
							}
						}
					}
					if (kn && !cn && v) {
						tot += k1.first;
						res.push_back(k1.second);
						kn--;
						v--;
					}
					while (cn && v >= 2) {
						tot += c.first;
						res.push_back(c.second);
						cn--;
						v -= 2;

						if (cn > 0) {
							c = catamarans.top();
							catamarans.pop();
						}
					}
				}
			}
		}
	}

	cout<<tot<<endl;
	if (res.size()) {
		cout<<res[0];
		for (i=1; i<res.size(); i++) cout<<" "<<res[i];
	}
	cout<<endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <utility>
#include <list>
#include <queue>
#include <deque>
#include <iterator>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>

#define MAX_INT 2147483647
#define MAX_LONG 9223372036854775807
#define MAX_ULONG 18446744073709551615

using namespace std;

priority_queue<pair<int, int> > kayacs, catamarans;
vector<int> res;

int main () {

    int n, v, t, p, i, j, tot, kn, cn, klim;

    cin>>n>>v;

    for (i=1; i<=n; i++) {
        cin>>t>>p;
        if (t == 1) kayacs.push(make_pair(p, i));
        else catamarans.push(make_pair(p, i));
    }

    pair<int, int> k1, k2, c;
    tot = 0;
    kn = kayacs.size();
    cn = catamarans.size();
    if (v % 2 == 1) {
        tot += kayacs.top().first;
        res.push_back(kayacs.top().second);
        kayacs.pop();
        kn--;
        v--;
    }
    if (!kayacs.empty()) {
        k1 = kayacs.top();
        kayacs.pop();
    }
    if (!kayacs.empty()) {
        k2 = kayacs.top();
        kayacs.pop();
    }
    c = catamarans.top();
    catamarans.pop();

    while (v > 1 && kn > 1 && cn > 0) {

        if (k1.first + k2.first > c.first) {
            tot = tot + k1.first + k2.first;
            res.push_back(k1.second);
            res.push_back(k2.second);
            kn -= 2;
            v -= 2;

            if (kn > 0) {
                k1 = kayacs.top();
                kayacs.pop();

                if (kn > 1) {
                    k2 = kayacs.top();
                    kayacs.pop();
                }
            }
        } else {
            tot += c.first;
            res.push_back(c.second);
            cn--;
            v -= 2;

            if (cn > 0) {
                c = catamarans.top();
                catamarans.pop();
            }
        }
    }

    if (v != 0) {

        if (cn == 0) {
            if (kn >= 2) kayacs.push(k2);
            while (kn && v) {
                tot += k1.first;
                res.push_back(k1.second);
                kn--;
                v--;

                if (kn) {
                    k1 = kayacs.top();
                    kayacs.pop();
                }
            }
        } else {

            if (kn == 0) {
                while (cn && v > 1) {
                    tot += c.first;
                    res.push_back(c.second);
                    cn--;
                    v -= 2;

                    if (cn > 0) {
                        c = catamarans.top();
                        catamarans.pop();
                    }
                }
            } else {

                if (kn == 1) {
                    while (kn && cn && v > 1) {
                        if (k1.first > c.first) {
                            tot += k1.first;
                            res.push_back(k1.second);
                            kn--;
                            v--;
                        } else {
                            tot += c.first;
                            res.push_back(c.second);
                            cn--;
                            v -= 2;

                            if (cn > 0) {
                                c = catamarans.top();
                                catamarans.pop();
                            }
                        }
                    }
                    if (kn && !cn && v) {
                        tot += k1.first;
                        res.push_back(k1.second);
                        kn--;
                        v--;
                    }
                    while (cn && v >= 2) {
                        tot += c.first;
                        res.push_back(c.second);
                        cn--;
                        v -= 2;

                        if (cn > 0) {
                            c = catamarans.top();
                            catamarans.pop();
                        }
                    }
                }
            }
        }
    }

    cout<<tot<<endl;
    if (res.size()) {
        cout<<res[0];
        for (i=1; i<res.size(); i++) cout<<" "<<res[i];
    }
    cout<<endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <utility>
#include <list>
#include <queue>
#include <deque>
#include <iterator>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>

#define MAX_INT 2147483647
#define MAX_LONG 9223372036854775807
#define MAX_ULONG 18446744073709551615

using namespace std;

priority_queue<pair<int, int> > kayacs, catamarans;
vector<int> res;

int main () {

    int n, v, i, j, id, p, cn = 0, kn = 0, tot, klim;

    cin>>n>>v;

    for (i=1; i<=n; i++) {

        cin>>id>>p;

        if (id == 1) {
            kn++;
            kayacs.push(make_pair(p, i));
        } else {
            cn++;
            catamarans.push(make_pair(p, i));
        }

    }

    tot = 0;
    pair<int, int> c, k1, k2;
    k1 = kayacs.top();
    kayacs.pop();
    if (!kayacs.empty()) {
        k2 = kayacs.top();
        kayacs.pop();
    }
    c = catamarans.top();
    catamarans.pop();
    klim = 1;
    if ((v-kn) % 2 == 1 && v % 2 == 1) klim += 1;
    while (v > 1 && kn > klim && cn) {

        if (k1.first + k2.first > c.first) {
            res.push_back(k1.second);
            tot += k1.first;
            res.push_back(k2.second);
            tot += k2.first;

            kn -= 2;

            if (kn >= 1) {
                k1 = kayacs.top();
                kayacs.pop();
                if (kn >= 2) {
                    k2 = kayacs.top();
                    kayacs.pop();
                }
            }
        } else {
            res.push_back(c.second);
            tot += c.first;

            cn--;

            if (cn >= 1) {
                c = catamarans.top();
                catamarans.pop();
            }
        }

        v -= 2;
    }

    if ((v-kn) % 2 == 1 && v % 2 == 1) {
        if (kn == 1) {
            res.push_back(k1.second);
            tot += k1.first;
            v--;
            kn--;

            while (cn && v > 1) {
                res.push_back(c.second);
                tot += c.first;

                cn--;

                if (cn >= 1) {
                    c = catamarans.top();
                    catamarans.pop();
                }

                v -= 2;
            }
        } else if (kn >= 2) {
            if (v == 1) {
                res.push_back(k1.second);
                tot += k1.first;
                v--;
                kn--;
            } else if (cn == 0) {
                kayacs.push(k2);
                while (kn && v) {
                    res.push_back(k1.second);
                    tot += k1.first;

                    kn--;

                    if (kn >= 1) {
                        k1 = kayacs.top();
                        kayacs.pop();
                    }

                    v--;
                }
            } else {
                while (v > 1 && kn && cn) {
                    if (k2.first > c.first) {
                        res.push_back(k1.second);
                        tot += k1.first;
                        res.push_back(k2.second);
                        tot += k2.first;

                        kn -= 2;
                    } else {
                        res.push_back(c.second);
                        tot += c.first;

                        cn--;

                        if (cn >= 1) {
                            c = catamarans.top();
                            catamarans.pop();
                        }
                    }

                    v -= 2;
                }
            }
        }
    }

    if (kn <= 1 && cn > 0 && v >= 2) {
        while (v >= 2 && cn) {
            res.push_back(c.second);
            tot += c.first;

            cn--;

            if (cn >= 1) {
                c = catamarans.top();
                catamarans.pop();
            }

            v -= 2;
        }

    } else if ((cn == 0 || (cn > 0 && v == 1)) && kn && v) {

        if (kn >= 2) kayacs.push(k2);

        while (v && kn) {

            res.push_back(k1.second);
            tot += k1.first;

            kn--;

            if (kn >= 1) {
                k1 = kayacs.top();
                kayacs.pop();
            }

            v--;
        }
    }

    if (v == 1 && kn) {
        res.push_back(k1.second);
        tot += k1.first;
        v--;
    }

    cout<<tot<<endl;

    cout<<res[0];
    for (i=1; i<res.size(); i++) cout<<" "<<res[i];
    cout<<endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <utility>
#include <list>
#include <queue>
#include <deque>
#include <iterator>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>

#define MAX_INT 2147483647
#define MAX_LONG 9223372036854775807
#define MAX_ULONG 18446744073709551615

using namespace std;

priority_queue<pair<int, int> > kayacs, catamarans;
vector<int> res;

int main () {

    int n, v, i, j, id, p, cn = 0, kn = 0, tot, klim;

    cin>>n>>v;

    for (i=1; i<=n; i++) {

        cin>>id>>p;

        if (id == 1) {
            kn++;
            kayacs.push(make_pair(p, i));
        } else {
            cn++;
            catamarans.push(make_pair(p, i));
        }

    }

    tot = 0;
    pair<int, int> c, k1, k2;
    k1 = kayacs.top();
    kayacs.pop();
    if (!kayacs.empty()) {
        k2 = kayacs.top();
        kayacs.pop();
    }
    c = catamarans.top();
    catamarans.pop();
    klim = 1 + ((v-kn) % 2);
    while (v > 1 && kn > klim && cn) {

        if (k1.first + k2.first > c.first) {
            res.push_back(k1.second);
            tot += k1.first;
            res.push_back(k2.second);
            tot += k2.first;

            kn -= 2;

            if (kn >= 1) {
                k1 = kayacs.top();
                kayacs.pop();
                if (kn >= 2) {
                    k2 = kayacs.top();
                    kayacs.pop();
                }
            }
        } else {
            res.push_back(c.second);
            tot += c.first;

            cn--;

            if (cn >= 1) {
                c = catamarans.top();
                catamarans.pop();
            }
        }

        v -= 2;
    }

    if ((v-kn) % 2 != 0) {
        if (kn == 1) {
            res.push_back(k1.second);
            tot += k1.first;
            v--;
            kn--;

            while (cn && v > 1) {
                res.push_back(c.second);
                tot += c.first;

                cn--;

                if (cn >= 1) {
                    c = catamarans.top();
                    catamarans.pop();
                }

                v -= 2;
            }
        } else if (kn >= 2) {
            if (v == 1) {
                res.push_back(k1.second);
                tot += k1.first;
                v--;
                kn--;
            } else if (cn == 0) {
                kayacs.push(k2);
                while (kn && v) {
                    res.push_back(k1.second);
                    tot += k1.first;

                    kn--;

                    if (kn >= 1) {
                        k1 = kayacs.top();
                        kayacs.pop();
                    }

                    v--;
                }
            } else {
                while (v > 1 && kn && cn) {
                    if (k2.first > c.first) {
                        res.push_back(k1.second);
                        tot += k1.first;
                        res.push_back(k2.second);
                        tot += k2.first;

                        kn -= 2;
                    } else {
                        res.push_back(c.second);
                        tot += c.first;

                        cn--;

                        if (cn >= 1) {
                            c = catamarans.top();
                            catamarans.pop();
                        }
                    }

                    v -= 2;
                }
            }
        }
    }

    if (kn <= 1 && cn > 0 && v >= 2) {
        while (v >= 2 && cn) {
            res.push_back(c.second);
            tot += c.first;

            cn--;

            if (cn >= 1) {
                c = catamarans.top();
                catamarans.pop();
            }

            v -= 2;
        }

    } else if ((cn == 0 || (cn > 0 && v == 1)) && kn && v) {

        if (kn >= 2) kayacs.push(k2);

        while (v && kn) {

            res.push_back(k1.second);
            tot += k1.first;

            kn--;

            if (kn >= 1) {
                k1 = kayacs.top();
                kayacs.pop();
            }

            v--;
        }
    }

    if (v == 1 && kn) {
        res.push_back(k1.second);
        tot += k1.first;
        v--;
    }

    cout<<tot<<endl;

    cout<<res[0];
    for (i=1; i<res.size(); i++) cout<<" "<<res[i];
    cout<<endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <utility>
#include <list>
#include <queue>
#include <deque>
#include <iterator>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>

#define MAX_INT 2147483647
#define MAX_LONG 9223372036854775807
#define MAX_ULONG 18446744073709551615

using namespace std;

priority_queue<pair<int, int> > kayacs, catamarans;
priority_queue<int, vector<int>, greater<int> > res;

int main () {

    int n, v, i, j, id, p, cn = 0, kn = 0, tot;

    cin>>n>>v;

    for (i=1; i<=n; i++) {

        cin>>id>>p;

        if (id == 1) {
            kn++;
            kayacs.push(make_pair(p, i));
        } else {
            cn++;
            catamarans.push(make_pair(p, i));
        }

    }

    tot = 0;
    pair<int, int> c, k1, k2;
    k1 = kayacs.top();
    kayacs.pop();
    if (!kayacs.empty()) {
        k2 = kayacs.top();
        kayacs.pop();
    }
    c = catamarans.top();
    catamarans.pop();
    while (v > 1 && kn > 1 && cn) {
        if (k1.first + k2.first > c.first) {
            res.push(k1.second);
            tot += k1.first;
            res.push(k2.second);
            tot += k2.first;

            kn -= 2;

            if (kn >= 1) {
                k1 = kayacs.top();
                kayacs.pop();
                if (kn >= 2) {
                    k2 = kayacs.top();
                    kayacs.pop();
                }
            }
        } else {
            res.push(c.second);
            tot += c.first;

            cn--;

            if (cn >= 1) {
                c = catamarans.top();
                catamarans.pop();
            }
        }

        v -= 2;
    }

    if (kn == 0 && cn > 0 && v >= 2) {
        while (v >= 2 && cn) {
            res.push(c.second);
            tot += c.first;

            cn--;

            if (cn >= 1) {
                c = catamarans.top();
                catamarans.pop();
            }

            v -= 2;
        }

    } else if (cn == 0 && kn && v) {

        if (kn >= 2) kayacs.push(k2);

        while (v && kn) {

            res.push(k1.second);
            tot += k1.first;

            kn--;

            if (kn >= 1) {
                k1 = kayacs.top();
                kayacs.pop();
            }

            v--;
        }
    }

    cout<<tot<<endl;

    cout<<res.top();
    res.pop();
    while (!res.empty()) {
        cout<<res.top()<<" ";
        res.pop();
    }
    cout<<endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <utility>
#include <list>
#include <queue>
#include <deque>
#include <iterator>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>

#define MAX_INT 2147483647
#define MAX_LONG 9223372036854775807
#define MAX_ULONG 18446744073709551615

using namespace std;

priority_queue<pair<int, int> > kayacs, catamarans;
vector<int> res;

int main () {

    int n, v, t, p, i, j, tot, kn, cn, klim;

    cin>>n>>v;

    for (i=1; i<=n; i++) {
        cin>>t>>p;
        if (t == 1) kayacs.push(make_pair(p, i));
        else catamarans.push(make_pair(p, i));
    }

    pair<int, int> k1, k2, c;
    tot = 0;
    kn = kayacs.size();
    cn = catamarans.size();
    if (v % 2 == 1) {
        tot += kayacs.top().first;
        res.push_back(kayacs.top().second);
        kayacs.pop();
        kn--;
        v--;
    }
    if (!kayacs.empty()) {
        k1 = kayacs.top();
        kayacs.pop();
    }
    if (!kayacs.empty()) {
        k2 = kayacs.top();
        kayacs.pop();
    }
    c = catamarans.top();
    catamarans.pop();

    while (v > 1 && kn > 1 && cn > 0) {

        if (k1.first + k2.first > c.first) {
            tot = tot + k1.first + k2.first;
            res.push_back(k1.second);
            res.push_back(k2.second);
            kn -= 2;
            v -= 2;

            if (kn > 0) {
                k1 = kayacs.top();
                kayacs.pop();

                if (kn > 1) {
                    k2 = kayacs.top();
                    kayacs.pop();
                }
            }
        } else {
            tot += c.first;
            res.push_back(c.second);
            cn--;
            v -= 2;

            if (cn > 0) {
                c = catamarans.top();
                catamarans.pop();
            }
        }
    }

    if (v != 0) {

        if (cn == 0) {
            if (kn >= 2) kayacs.push(k2);
            while (kn && v) {
                tot += k1.first;
                res.push_back(k1.second);
                kn--;
                v--;

                if (kn) {
                    k1 = kayacs.top();
                    kayacs.pop();
                }
            }
        } else {

            if (kn == 0) {
                while (cn && v > 1) {
                    tot += c.first;
                    res.push_back(c.second);
                    cn--;
                    v -= 2;

                    if (cn > 0) {
                        c = catamarans.top();
                        catamarans.pop();
                    }
                }
            } else {

                if (kn == 1) {
                    while (kn && cn && v > 1) {
                        if (k1.first > c.first) {
                            tot += k1.first;
                            res.push_back(k1.second);
                            kn--;
                            v--;
                        } else {
                            tot += c.first;
                            res.push_back(c.second);
                            cn--;
                            v -= 2;

                            if (cn > 0) {
                                c = catamarans.top();
                                catamarans.pop();
                            }
                        }
                    }
                    if (kn && !cn && v) {
                        tot += k1.first;
                        res.push_back(k1.second);
                        kn--;
                        v--;
                    }
                    while (cn && v >= 2) {
                        tot += c.first;
                        res.push_back(c.second);
                        cn--;
                        v -= 2;

                        if (cn > 0) {
                            c = catamarans.top();
                            catamarans.pop();
                        }
                    }
                }
            }
        }
    }

    cout<<tot<<endl;
    cout<<res[0];
    for (i=1; i<res.size(); i++) cout<<" "<<res[i];
    cout<<endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <utility>
#include <list>
#include <queue>
#include <deque>
#include <iterator>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>

#define MAX_INT 2147483647
#define MAX_LONG 9223372036854775807
#define MAX_ULONG 18446744073709551615

using namespace std;

priority_queue<pair<int, int> > kayacs, catamarans;
vector<int> res;

int main () {

	int n, v, t, p, i, j, tot, kn, cn, klim;

	cin>>n>>v;

	for (i=1; i<=n; i++) {
		cin>>t>>p;
		if (t == 1) kayacs.push(make_pair(p, i));
		else catamarans.push(make_pair(p, i));
	}

	pair<int, int> k1, k2, c;
	tot = 0;
	kn = kayacs.size();
	cn = catamarans.size();
	if (v % 2 == 1) {
		tot += kayacs.top().first;
		res.push_back(kayacs.top().second);
		kayacs.pop();
		kn--;
		v--;
	}
	if (!kayacs.empty()) {
		k1 = kayacs.top();
		kayacs.pop();
	}
	if (!kayacs.empty()) {
		k2 = kayacs.top();
		kayacs.pop();
	}
	c = catamarans.top();
	catamarans.pop();

	while (v > 1 && kn > 1 && cn > 0) {

		if (k1.first + k2.first > c.first) {
			tot = tot + k1.first + k2.first;
			res.push_back(k1.second);
			res.push_back(k2.second);
			kn -= 2;
			v -= 2;

			if (kn > 0) {
				k1 = kayacs.top();
				kayacs.pop();

				if (kn > 1) {
					k2 = kayacs.top();
					kayacs.pop();
				}
			}
		} else {
			tot += c.first;
			res.push_back(c.second);
			cn--;
			v -= 2;

			if (cn > 0) {
				c = catamarans.top();
				catamarans.pop();
			}
		}
	}

	if (v != 0) {

		if (cn == 0) {
			if (kn >= 2) kayacs.push(k2);
			while (kn && v) {
				tot += k1.first;
				res.push_back(k1.second);
				kn--;
				v--;

				if (kn) {
					k1 = kayacs.top();
					kayacs.pop();
				}
			}
		} else {

			if (kn == 0) {
				while (cn && v >= 2) {
					tot += c.first;
					res.push_back(c.second);
					cn--;
					v -= 2;

					if (cn > 0) {
						c = catamarans.top();
						catamarans.pop();
					}
				}
			} else {

				if (kn == 1) {
					while (kn && cn && v >= 2) {
						if (k1.first > c.first) {
							tot += k1.first;
							res.push_back(k1.second);
							kn--;
							v--;
						} else {
							tot += c.first;
							res.push_back(c.second);
							cn--;
							v -= 2;

							if (cn > 0) {
								c = catamarans.top();
								catamarans.pop();
							}
						}
					}
					while (cn && v >= 2) {
						tot += c.first;
						res.push_back(c.second);
						cn--;
						v -= 2;

						if (cn > 0) {
							c = catamarans.top();
							catamarans.pop();
						}
					}
				}
			}
		}
	}

	cout<<tot<<endl;
	cout<<res[0];
	for (i=1; i<res.size(); i++) cout<<" "<<res[i];
	cout<<endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <utility>
#include <list>
#include <queue>
#include <deque>
#include <iterator>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>

#define MAX_INT 2147483647
#define MAX_LONG 9223372036854775807
#define MAX_ULONG 18446744073709551615

using namespace std;

priority_queue<pair<int, int> > kayacs, catamarans;
priority_queue<int, vector<int>, greater<int> > res;

int main () {

    int n, v, i, j, id, p, cn = 0, kn = 0, tot;

    cin>>n>>v;

    for (i=1; i<=n; i++) {

        cin>>id>>p;

        if (id == 1) {
            kn++;
            kayacs.push(make_pair(p, i));
        } else {
            cn++;
            catamarans.push(make_pair(p, i));
        }

    }

    tot = 0;
    pair<int, int> c, k1, k2;
    k1 = kayacs.top();
    kayacs.pop();
    if (!kayacs.empty()) {
        k2 = kayacs.top();
        kayacs.pop();
    }
    c = catamarans.top();
    catamarans.pop();
    while (v > 1 && kn > 1 && cn) {
        if (k1.first + k2.first > c.first) {
            res.push(k1.second);
            tot += k1.first;
            res.push(k2.second);
            tot += k2.first;

            kn -= 2;

            if (kn >= 1) {
                k1 = kayacs.top();
                kayacs.pop();
                if (kn >= 2) {
                    k2 = kayacs.top();
                    kayacs.pop();
                }
            }
        } else {
            res.push(c.second);
            tot += c.first;

            cn--;

            if (cn >= 1) {
                c = catamarans.top();
                catamarans.pop();
            }
        }

        v -= 2;
    }

    if (kn == 0 && cn > 0 && v >= 2) {
        while (v >= 2 && cn) {
            res.push(c.second);
            tot += c.first;

            cn--;

            if (cn >= 1) {
                c = catamarans.top();
                catamarans.pop();
            }

            v -= 2;
        }

    } else if (cn == 0 && kn && v) {

        if (kn >= 2) kayacs.push(k2);

        while (v && kn) {

            res.push(k1.second);
            tot += k1.first;

            kn--;

            if (kn >= 1) {
                k1 = kayacs.top();
                kayacs.pop();
            }

            v--;
        }
    } else if (v == 1 && kn) {
        res.push(k1.second);
        tot += k1.first;
        v--;
    }

    cout<<tot<<endl;

    cout<<res.top();
    res.pop();
    while (!res.empty()) {
        cout<<" "<<res.top();
        res.pop();
    }
    cout<<endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <utility>
#include <list>
#include <queue>
#include <deque>
#include <iterator>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>

#define MAX_INT 2147483647
#define MAX_LONG 9223372036854775807
#define MAX_ULONG 18446744073709551615

using namespace std;

priority_queue<pair<int, int> > kayacs, catamarans;
priority_queue<int, vector<int>, greater<int> > res;

int main () {

    int n, v, i, j, id, p, cn = 0, kn = 0, tot;

    cin>>n>>v;

    for (i=1; i<=n; i++) {

        cin>>id>>p;

        if (id == 1) {
            kn++;
            kayacs.push(make_pair(p, i));
        } else {
            cn++;
            catamarans.push(make_pair(p, i));
        }

    }

    tot = 0;
    pair<int, int> c, k1, k2;
    k1 = kayacs.top();
    kayacs.pop();
    if (!kayacs.empty()) {
        k2 = kayacs.top();
        kayacs.pop();
    }
    c = catamarans.top();
    catamarans.pop();
    while (v > 1 && kn > 1 && cn) {
        if (k1.first + k2.first > c.first) {
            res.push(k1.second);
            tot += k1.first;
            res.push(k2.second);
            tot += k2.first;

            kn -= 2;

            if (kn >= 1) {
                k1 = kayacs.top();
                kayacs.pop();
                if (kn >= 2) {
                    k2 = kayacs.top();
                    kayacs.pop();
                }
            }
        } else {
            res.push(c.second);
            tot += c.first;

            cn--;

            if (cn >= 1) {
                c = catamarans.top();
                catamarans.pop();
            }
        }

        v -= 2;
    }

    if (kn <= 1 && cn > 0 && v >= 2) {
        while (v >= 2 && cn) {
            res.push(c.second);
            tot += c.first;

            cn--;

            if (cn >= 1) {
                c = catamarans.top();
                catamarans.pop();
            }

            v -= 2;
        }

    } else if ((cn == 0 || (cn > 0 && v == 1)) && kn && v) {

        if (kn >= 2) kayacs.push(k2);

        while (v && kn) {

            res.push(k1.second);
            tot += k1.first;

            kn--;

            if (kn >= 1) {
                k1 = kayacs.top();
                kayacs.pop();
            }

            v--;
        }
    }

    if (v == 1 && kn) {
        res.push(k1.second);
        tot += k1.first;
        v--;
    }

    cout<<tot<<endl;

    cout<<res.top();
    res.pop();
    while (!res.empty()) {
        cout<<" "<<res.top();
        res.pop();
    }
    cout<<endl;

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef pair<int, int> PII;

vector<PII> vec[2];

int main()
{
    int n, v;

    scanf("%d%d", &n, &v);
    for(int i = 0; i < n; i++)
    {
        int t, p;
        scanf("%d%d", &t, &p);
        t--;
        vec[t].push_back(PII(p, i + 1));
    }

    sort(vec[0].begin(), vec[0].end(), greater<PII>());
    sort(vec[1].begin(), vec[1].end(), greater<PII>());

    int now = 0, ans = 0, p = -1;
    for(int i = 0; i < vec[1].size() && v >= 2; i++)
    {
        now += vec[1][i].first;
        p = i;
        v -= 2;
    }
    ans = now;
    int pos[2] = {-1, p};
    for(int i = 0; i < vec[0].size(); i++)
    {
        now += vec[0][i].first;
        v--;
        if(p >= 0 && v < 0)
        {
            now -= vec[1][p--].first;
            v += 2;
        }
        if(v >= 0 && now > ans)
        {
            ans = now;
            pos[0] = i;
            pos[1] = p;
        }
        if(v == 0 && now < ans)
            break;
    }

    printf("%d\n", ans);
    for(int i = 0; i <= pos[0]; i++)
        printf("%d ", vec[0][i].second);
    for(int i = 0; i <= pos[1]; i++)
        printf("%d ", vec[1][i].second);
}


#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;

struct Boat
{
    int t;
    int p;
    int sub;
}boat[100000];

bool cmp(Boat a, Boat b)
{
    return a.p * b.t > b.p * a.t;
}

int main()
{
    int n, v;
    cin >> n >> v;

    for(int i = 0; i < n; i++)
        {cin >> boat[i].t >> boat[i].p; boat[i].sub = i + 1;}
    sort(boat , boat + n , cmp);

    int sum = 0, i = 0;
    stack<Boat> s1, s2;
    for(; i < n && (v -= boat[i].t) >= 0; i++)
    {
        sum += boat[i].p;
        if(boat[i].t == 1)
            s1.push(boat[i]);
        else
            s2.push(boat[i]);
    }
    if(i < n && v + boat[i].t == 1)
    {
        if(s1.empty())
        {
            for(; i < n; i++)
                if(boat[i].t == 1)
                    {sum += boat[i].p; s1.push(boat[i]);}
        }
        else
        {
            Boat a, b = {0,0,0}, c = {0,0,0};
            a = s1.top();
            for(; i < n && (b.t == 0 || c.t == 0); i++)
            {
                if(b.t == 0 && boat[i].t == 1)
                    b = boat[i];
                if(c.t == 0 && boat[i].t == 2)
                    c = boat[i];
            }
            if(a.p + b.p <= c.p)
                {s1.pop(); s2.push(c); sum = sum - a.p + c.p;}
            else if(b.t != 0)
                {s1.push(b); sum += b.p;}
        }
    }
    cout << sum << endl;
    while(!s1.empty())
        {cout << s1.top().sub << ' '; s1.pop();}
    while(!s2.empty())
        {cout << s2.top().sub << ' '; s2.pop();}

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;

struct Boat
{
    int t;
    int p;
    int id;
}a[100000];

bool cmp(struct Boat a,struct Boat b)
{
    if(a.p*b.t == b.p*a.t)
        return a.t > b.t;
    else
        return a.p*b.t > b.p*a.t;
}

int main()
{
    int n, v, sum = 0;
    vector <int> id;

    scanf("%d%d", &n, &v);
    for(int i = 0; i < n; i++)
    {
        scanf("%d%d", &a[i].t, &a[i].p);
        a[i].id = i + 1;
    }
    sort(a, a + n, cmp);

    int i;
    for(i = 0; i < n && v != 0; i++)
    {
        if(v == 2 && a[i].t != 2)
            break;
        if(v >= a[i].t)
        {
            sum += a[i].p;
            v -= a[i].t;
            id.push_back(a[i].id);
        }
    }

    int sum1 = 0, sum2 = 0;
    int plan1_id[2], plan2_id;
    bool plan1 = false, plan2 = false;
    if(v == 2 && i < n)
    {
        sum1 += a[i].p;
        plan1_id[0] = a[i].id;
        for(i++; i < n; i++)
        {
            if(plan1 && plan2)
                break;
            if(a[i].t == 1 && plan1 == false)
            {
                plan1 = true;
                sum1 += a[i].p;
                plan1_id[1] = a[i].id;
            }
            if(a[i].t == 2 && plan2 == false)
            {
                plan2 = true;
                sum2 += a[i].p;
                plan2_id = a[i].id;
            }
        }
        if(sum1 <= sum2 && sum2 != 0)
        {
            sum += sum2;
            id.push_back(plan2_id);
        }
        else
        {
            sum += sum1;
            id.push_back(plan1_id[0]);
            id.push_back(plan1_id[1]);
        }
    }

    printf("%d\n", sum);
    for(int i = 0; i < id.size(); i++)
        printf("%d\n", id[i]);

    return 0;
}


#include <iostream>
#include <queue>
using namespace std;

struct Boat
{
    int t;
    int p;
    int sub;
};

bool operator<(Boat a, Boat b)
{
    return a.p < b.p;
}

int main()
{
    int n, v;
    cin >> n >> v;

    priority_queue<Boat> q1, q2;
    for(int i = 0; i < n; i++)
    {
        Boat mid;
        cin >> mid.t >> mid.p;
        mid.sub = i + 1;

        if(mid.t == 1) q1.push(mid);
        if(mid.t == 2) q2.push(mid);
    }

    int sum = 0;
    deque<Boat> ans;
    while((v -= 2) >= 0 && !q2.empty())
        {ans.push_back(q2.top()); sum += q2.top().p; q2.pop();}
    v += 2;
    while(--v >= 0 && !q1.empty())
        {ans.push_front(q1.top()); sum += q1.top().p; q1.pop();}

    while(q1.size() > 2 && !q2.empty())
    {
        Boat a = q1.top(); q1.pop();
        Boat b = q1.top(); q1.pop();
        if(a.p + b.p <= ans.back().p)
            break;
        ans.pop_back(); ans.push_front(a); ans.push_front(b);
    }
    if(q1.size() == 1 && !q2.empty() && q1.top().p > ans.back().p)
        {ans.pop_back(); ans.push_front(q1.top());}

    cout << sum << endl;
    while(!ans.empty())
        {cout << ans.front().sub << ' '; ans.pop_front();}

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef pair<int, int> PII;

vector<PII> vec[2];

int main()
{
    int n, v;

    scanf("%d%d", &n, &v);
    for(int i = 0; i < n; i++)
    {
        int t, p;
        scanf("%d%d", &t, &p);
        t--;
        vec[t].push_back(PII(p, i + 1));
    }

    sort(vec[0].begin(), vec[0].end(), greater<PII>());
    sort(vec[1].begin(), vec[1].end(), greater<PII>());

    int now = 0, ans = 0, p = -1;
    for(int i = 0; i < vec[1].size() && v >= 2; i++)
    {
        now += vec[1][i].first;
        p = i;
        v -= 2;
    }
    ans = now;
    int pos[2] = {-1, p};
    for(int i = 0; i < vec[0].size(); i++)
    {
        now += vec[0][i].first;
        v--;
        for(; p >= 0 && v < 0; p--)
        {
            now -= vec[1][p].first;
            v += 2;
        }
        if(v >= 0 && now > ans)
        {
            ans = now;
            pos[0] = i;
            pos[1] = p;
        }
    }

    printf("%d\n", ans);
    for(int i = 0; i <= pos[0]; i++)
        printf("%d ", vec[0][i].second);
    for(int i = 0; i <= pos[1]; i++)
        printf("%d ", vec[1][i].second);
}


#include <iostream>
#include <queue>
using namespace std;

struct Boat
{
    int t;
    int p;
    int sub;
};

bool operator<(Boat a, Boat b)
{
    return a.p < b.p;
}

int main()
{
    int n, v;
    cin >> n >> v;

    priority_queue<Boat> q1, q2;
    for(int i = 0; i < n; i++)
    {
        Boat mid;
        cin >> mid.t >> mid.p;
        mid.sub = i + 1;

        if(mid.t == 1) q1.push(mid);
        if(mid.t == 2) q2.push(mid);
    }

    int sum = 0;
    deque<Boat> ans;
    while((v -= 2) >= 0 && !q2.empty())
        {ans.push_back(q2.top()); sum += q2.top().p; q2.pop();}
    v += 2;
    while(--v >= 0 && !q1.empty())
        {ans.push_front(q1.top()); sum += q1.top().p; q1.pop();}

    while(q1.size() > 1 && !q2.empty())
    {
        Boat a = q1.top(); q1.pop();
        Boat b = q1.top(); q1.pop();
        if(a.p + b.p <= ans.back().p)
            break;
        sum += a.p + b.p - ans.back().p;
        ans.pop_back(); ans.push_front(a); ans.push_front(b);
    }
    if(q1.size() == 1 && !q2.empty() && q1.top().p > ans.back().p)
        {sum += q1.top().p - ans.back().p; ans.pop_back(); ans.push_front(q1.top());}

    cout << sum << endl;
    while(!ans.empty())
        {cout << ans.front().sub << ' '; ans.pop_front();}

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef pair<int, int> PII;

vector<PII> vec[2];

int main()
{
    int n, v;

    scanf("%d%d", &n, &v);
    for(int i = 0; i < n; i++)
    {
        int t, p;
        scanf("%d%d", &t, &p);
        t--;
        vec[t].push_back(PII(p, i + 1));
    }

    sort(vec[0].begin(), vec[0].end(), greater<PII>());
    sort(vec[1].begin(), vec[1].end(), greater<PII>());

    int now = 0, ans = 0, p = -1;
    for(int i = 0; i < vec[1].size() && v >= 2; i++)
    {
        now += vec[1][i].first;
        p = i;
        v -= 2;
    }
    ans = now;
    int pos[2] = {-1, p};
    for(int i = 0; i < vec[0].size(); i++)
    {
        now += vec[0][i].first;
        v--;
        if(p >= 0 && v < 0)
        {
            now -= vec[1][p--].first;
            v += 2;
        }
        if(v >= 0 && now > ans)
        {
            ans = now;
            pos[0] = i;
            pos[1] = p;
        }
    }

    printf("%d\n", ans);
    for(int i = 0; i <= pos[0]; i++)
        printf("%d ", vec[0][i].second);
    for(int i = 0; i <= pos[1]; i++)
        printf("%d ", vec[1][i].second);
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef pair<int, int> PII;

vector<PII> vec[2];

int main()
{
    int n, v;

    scanf("%d%d", &n, &v);
    for(int i = 0; i < n; i++)
    {
        int t, p;
        scanf("%d%d", &t, &p);
        t--;
        vec[t].push_back(PII(p, i + 1));
    }

    sort(vec[0].begin(), vec[0].end(), greater<PII>());
    sort(vec[1].begin(), vec[1].end(), greater<PII>());

    int now = 0, ans = 0, p = -1;
    for(int i = 0; i < vec[1].size() && v >= 2; i++)
    {
        now += vec[1][i].first;
        p = i;
        v -= 2;
    }
    ans = now;
    int pos[2] = {-1, p};
    for(int i = 0; i < vec[0].size(); i++)
    {
        now += vec[0][i].first;
        v--;
        if(p >= 0 && v < 0)
        {
            now -= vec[1][p--].first;
            v += 2;
        }
        if(v >= 0 && now > ans)
        {
            ans = now;
            pos[0] = i;
            pos[1] = p;
         }
    }

    printf("%d\n", ans);
    for(int i = 0; i <= pos[0]; i++)
        printf("%d ", vec[0][i].second);
    for(int i = 0; i <= pos[1]; i++)
        printf("%d ", vec[1][i].second);
}


#include <iostream>
#include <queue>
using namespace std;

struct Boat
{
    int t;
    int p;
    int sub;
};

bool operator<(Boat a, Boat b)
{
    return a.p < b.p;
}

int main()
{
    int n, v;
    cin >> n >> v;

    priority_queue<Boat> q1, q2;
    for(int i = 0; i < n; i++)
    {
        Boat mid;
        cin >> mid.t >> mid.p;
        mid.sub = i + 1;

        if(mid.t == 1) q1.push(mid);
        if(mid.t == 2) q2.push(mid);
    }

    int sum = 0;
    deque<Boat> ans;
    while((v -= 2) >= 0 && !q2.empty())
        {ans.push_back(q2.top()); sum += q2.top().p; q2.pop();}
    v += 2;
    while(--v >= 0 && !q1.empty())
        {ans.push_front(q1.top()); sum += q1.top().p; q1.pop();}

    while(q1.size() > 1 && !q2.empty())
    {
        Boat a = q1.top(); q1.pop();
        Boat b = q1.top(); q1.pop();
        if(a.p + b.p <= ans.back().p)
            break;
        ans.pop_back(); ans.push_front(a); ans.push_front(b);
    }
    if(q1.size() == 1 && !q2.empty() && q1.top().p > ans.back().p)
        {ans.pop_back(); ans.push_front(q1.top());}

    cout << sum << endl;
    while(!ans.empty())
        {cout << ans.front().sub << ' '; ans.pop_front();}

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef pair<int, int> PII;

vector<PII> vec[2];

int main()
{
    int n, v;

    scanf("%d%d", &n, &v);
    for(int i = 0; i < n; i++)
    {
        int t, p;
        scanf("%d%d", &t, &p);
        t--;
        vec[t].push_back(PII(p, i + 1));
    }

    sort(vec[0].begin(), vec[0].end(), greater<PII>());
    sort(vec[1].begin(), vec[1].end(), greater<PII>());

    int now = 0, ans = 0, p = -1;
    for(int i = 0; i < vec[1].size() && v >= 2; i++)
    {
        now += vec[1][i].first;
        p = i;
        v -= 2;
    }
    ans = now;
    int pos[2] = {-1, p};
    for(int i = 0; i < vec[0].size(); i++)
    {
        now += vec[0][i].first;
        v--;
        if(p >= 0 && v < 0)
        {
            now -= vec[1][p--].first;
            v += 2;
        }
        if(v >= 0 && now > ans)
        {
            ans = now;
            pos[0] = i;
            pos[1] = p;
        }
        if(v >= 0 && now <= ans)
            break;
    }

    printf("%d\n", ans);
    for(int i = 0; i <= pos[0]; i++)
        printf("%d ", vec[0][i].second);
    for(int i = 0; i <= pos[1]; i++)
        printf("%d ", vec[1][i].second);
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef pair<int, int> PII;

vector<PII> vec[2];

int main()
{
    int n, v;

    scanf("%d%d", &n, &v);
    for(int i = 0; i < n; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        x--;
        vec[x].push_back(PII(y, i + 1));
    }

    sort(vec[0].begin(), vec[0].end(), greater<PII>());
    sort(vec[1].begin(), vec[1].end(), greater<PII>());

    int now = 0, ans = 0, p = -1;
    for(int i = 0; i < vec[1].size() && v >= 2; i++)
    {
        now += vec[1][i].first;
        p = i;
        v -= 2;
    }
    ans = now;
    int pos[2] = {-1, p};
    for(int i = 0; i < vec[0].size(); i++)
    {
        now += vec[0][i].first;
        v--;
        for(; p >= 0 && v < 0; p--)
        {
            now -= vec[1][p].first;
            v += 2;
        }
        if(v >= 0 && now > ans)
        {
            ans = now;
            pos[0] = i;
            pos[1] = p;
        }
    }

    printf("%d\n", ans);
    for(int i = 0; i <= pos[0]; i++)
        printf("%d ", vec[0][i].second);
    for(int i = 0; i <= pos[1]; i++)
        printf("%d ", vec[1][i].second);
}


#include<stdio.h>
#include<stdlib.h>
#include <algorithm>
using namespace  std;
typedef struct stu
 {
     int vi;
     int pi;
     float ave;
     int i;
 }student;
student date[110000];
int n,v,sumv=0,ans=0;

int Compare(student a, student b)
{
  //  return (
            if(b.ave<a.ave)return 1 ;
            return 0;
}
int main()
{   int i,p,t=0,t1,t2;
    scanf("%d %d",&n,&v);
    for(i=0;i<n;i++)
    {
        scanf("%d %d",&date[i].vi,&date[i].pi);
        date[i].ave=(int)date[i].pi/date[i].vi;
        date[i].i=i+1;
        sumv+=date[i].vi;
    }
    if(sumv<v) v=sumv;
    sort(date,date+n,Compare);
    p=0;
   // for(i=0;i<n;i++)printf("%d  %d    %f   \n",date[i].vi,date[i].pi,date[i].ave);
    while((!(v==2&&date[p].vi==1&&date[p+1].vi==2))&&v>0&&p<n)
    {
        if(v-date[p].vi>=0)
        {
          v-=date[p].vi;
          date[p].vi=- date[p].vi;
            ans+=date[p].pi;
        }
    p++;
    }
    if(v==2)
    {//printf("dsaf");
        t=-2;
        for(i=p+1;i<n;i++)
        if(date[i].vi==1){t=i;break;}
        if(t>=0)
        {
            if(date[p].pi+date[t].pi>date[p+1].pi)
            {
                ans+=date[p].pi+date[t].pi;
                date[p].vi=-date[p].vi;date[t].vi=-date[t].vi;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }
        }else
        {
            if(date[p].pi>date[p+1].pi)
            {
                ans+=date[p].pi;
                date[p].vi=-1;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }

        }
        }
    if(v==1)
    {
        for(i=1;i<n;i++)
            if(date[i].vi==-1)t1=i;
         for(i=n-1;i>=0;i--)
            if(date[i].vi==2)t2=i;
    if(date[t1].pi<date[t2].pi)
    {ans-=date[t1].pi-date[t2].pi;
     date[t1].vi=-date[t1].vi;
     date[t2].vi=-date[t2].vi;
     }
    }
        printf("%d\n",ans);
    for(i=0;i<n;i++)
        if(date[i].vi<0)
            printf("%d ",date[i].i);

    return 0;
}
/*5 3
1 9      9                     5 4
2 9     4.5                 3 1 5
1 9     9
2 10    5
1 6     6*/


#include<stdio.h>
#include<stdlib.h>
#include <algorithm>
using namespace  std;
typedef struct stu
 {
     float vi;
     float pi;
     float ave;
     int i;
 }student;
student date[110000];
int n,v,sumv=0,ans=0;

float Compare(student a, student b)
{
    return (b.ave-a.ave);
}
int main()
{   int i,p,t=0;

    scanf("%d %d",&n,&v);
    for(i=0;i<n;i++)
    {
        scanf("%f %f",&date[i].vi,&date[i].pi);
        date[i].ave=date[i].pi/date[i].vi;
        date[i].i=i;
        sumv+=date[i].vi;
    }
    if(sumv<v) v=sumv;
    sort(date,date+n,Compare);
    p=0;
    while((!(v==2&&date[p].vi==1&&date[p+1].vi==2))&&v>0)
    {
        if(v-date[p].vi>=0)
        {
          v-=date[p].vi;
          date[p].vi=-21;
            ans+=date[p].pi;
        }
    p++;
    }
    if(v==2)
    {
        t=-2;
        for(i=p+1;i<n;i++)
        if(date[i].vi==1){t=i;break;}
        if(t>=0)
        {
            if(date[p].pi+date[t].pi>date[p+1].pi)
            {
                ans+=date[p].pi+date[t].pi;
                date[p].vi=-1;date[t].vi=-1;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }
        }else
        {
            if(date[p].pi>date[p+1].pi)
            {
                ans+=date[p].pi;
                date[p].vi=-1;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }

        }
        }

    printf("%d\n",ans);
    for(i=0;i<n;i++)
        if(date[i].vi<0)
            printf("%d",date[i].i+1);

    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include <algorithm>
using namespace  std;
typedef struct stu
 {
     int vi;
     int pi;
     float ave;
     int i;
 }student;
student date[110000];
int n,v,sumv=0,ans=0;

int Compare(student a, student b)
{
  //  return (
            if(b.ave<a.ave)return 1 ;
            return 0;
}
int main()
{   int i,p,t,t1,t2;
    scanf("%d %d",&n,&v);
    t=n;
    date[n].pi=0;date[n].vi=0;
    for(i=0;i<n;i++)
    {
        scanf("%d %d",&date[i].vi,&date[i].pi);
        date[i].ave=(float)date[i].pi/date[i].vi;
        date[i].i=i+1;
        sumv+=date[i].vi;
    }
    if(sumv<v) v=sumv;
    sort(date,date+n,Compare);
    p=0;
   // for(i=0;i<n;i++)printf("%d  %d    %f   \n",date[i].vi,date[i].pi,date[i].ave);
    while((!(v==2&&date[p].vi==1&&date[p+1].vi==2))&&v>0&&p<n)
    {
        if(v-date[p].vi>=0)
        {
          v-=date[p].vi;
          date[p].vi=- date[p].vi;
            ans+=date[p].pi;
        }
    p++;
    }

    if(v==1)
    {t1=-1;t2=-1;
        for(i=1;i<n;i++)
            if(date[i].vi==-1)t1=i;
         for(i=n-1;i>=0;i--)
            if(date[i].vi==2)t2=i;
    if(date[t1].pi<date[t2].pi&&t1>=0&&t2>=0)
    {ans-=date[t1].pi-date[t2].pi;
     date[t1].vi=-date[t1].vi;
     date[t2].vi=-date[t2].vi;
     }
    }
        printf("%d\n",ans);
    for(i=0;i<n;i++)
        if(date[i].vi<0)
            printf("%d ",date[i].i);

    return 0;
}
/*5 3
1 9      9                     5 4
2 9     4.5                 3 1 5
1 9     9
2 10    5
1 6     6*/


#include<stdio.h>
#include<stdlib.h>
#include <algorithm>
using namespace  std;
typedef struct stu
 {
     int vi;
     int pi;
     float ave;
     int i;
 }student;
student date[1100000];
int n,v,sumv=0,ans=0;

int Compare(student a, student b)
{
    return (b.ave<a.ave);
}
int main()
{   int i,p,t=0;
    scanf("%d %d",&n,&v);
    for(i=0;i<n;i++)
    {
        scanf("%d %d",&date[i].vi,&date[i].pi);
        date[i].ave=(int)date[i].pi/date[i].vi;
        date[i].i=i+1;
        sumv+=date[i].vi;
    }
    if(sumv<v) v=sumv;
    sort(date,date+n,Compare);
    p=0;
 //   for(i=0;i<n;i++)printf("%f   ",date[i].ave);
    while((!(v==2&&date[p].vi==1&&date[p+1].vi==2))&&v>0)
    {
        if(v-date[p].vi>=0)
        {
          v-=date[p].vi;
          date[p].vi=-21;
            ans+=date[p].pi;
        }
    p++;
    }
    if(v==2)
    {
        t=-2;
        for(i=p+1;i<n;i++)
        if(date[i].vi==1){t=i;break;}
        if(t>=0)
        {
            if(date[p].pi+date[t].pi>date[p+1].pi)
            {
                ans+=date[p].pi+date[t].pi;
                date[p].vi=-1;date[t].vi=-1;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }
        }else
        {
            if(date[p].pi>date[p+1].pi)
            {
                ans+=date[p].pi;
                date[p].vi=-1;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }

        }
        }

    printf("%d\n",ans);
    for(i=0;i<n;i++)
        if(date[i].vi<0)
            printf("%d ",date[i].i);

    return 0;
}
/*5 3
1 9      9                     5 4
2 9     4.5                 3 1 5
1 9     9
2 10    5
1 6     6*/


#include<stdio.h>
#include<stdlib.h>
#include <algorithm>
using namespace  std;
typedef struct stu
 {
     int vi;
     int pi;
     float ave;
     int i;
 }student;
student date[110000];
int n,v,sumv=0,ans=0;

float Compare(student a, student b)
{
    return (b.ave-a.ave);
}
int main()
{   int i,p,t=0;

    scanf("%d %d",&n,&v);
    for(i=0;i<n;i++)
    {
        scanf("%d %d",&date[i].vi,&date[i].pi);
        date[i].ave=date[i].pi/date[i].vi;
        date[i].i=i;
        sumv+=date[i].vi;
    }
    if(sumv<v) v=sumv;
    sort(date,date+n,Compare);
    p=0;
    while((!(v==2&&date[p].vi==1&&date[p+1].vi==2))&&v>0)
    {
        if(v-date[p].vi>=0)
        {
          v-=date[p].vi;
          date[p].vi=-21;
            ans+=date[p].pi;
        }
    p++;
    }
    if(v==2)
    {
        t=-2;
        for(i=p+1;i<n;i++)
        if(date[i].vi==1){t=i;break;}
        if(t>=0)
        {
            if(date[p].pi+date[t].pi>date[p+1].pi)
            {
                ans+=date[p].pi+date[t].pi;
                date[p].vi=-1;date[t].vi=-1;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }
        }else
        {
            if(date[p].pi>date[p+1].pi)
            {
                ans+=date[p].pi;
                date[p].vi=-1;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }

        }
        }

    printf("%d\n",ans);
    for(i=0;i<n;i++)
        if(date[i].vi<0)
            printf("%d ",date[i].i+1);

    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include <algorithm>
using namespace  std;
typedef struct stu
 {
     int vi;
     int pi;
     float ave;
     int i;
 }student;
student date[110000];
int n,v,sumv=0,ans=0;

int Compare(student a, student b)
{
  //  return (
            if(b.ave<a.ave)return 1 ;
            return 0;
}
int main()
{   int i,p,t=0,t1,t2;
    scanf("%d %d",&n,&v);
    for(i=0;i<n;i++)
    {
        scanf("%d %d",&date[i].vi,&date[i].pi);
        date[i].ave=(float)date[i].pi/date[i].vi;
        date[i].i=i+1;
        sumv+=date[i].vi;
    }
    if(sumv<v) v=sumv;
    sort(date,date+n,Compare);
    p=0;
    //for(i=0;i<n;i++)printf("%d  %d    %f   \n",date[i].vi,date[i].pi,date[i].ave);
    while((!(v==2&&date[p].vi==1&&date[p+1].vi==2))&&v>0&&p<n)
    {
        if(v-date[p].vi>=0)
        {
          v-=date[p].vi;
          date[p].vi=- date[p].vi;
            ans+=date[p].pi;
        }
    p++;
    }
    if(v==2)
    {//printf("dsaf");
        t=-2;
        for(i=p+1;i<n;i++)
        if(date[i].vi==1){t=i;break;}
        if(t>=0)
        {
            if(date[p].pi+date[t].pi>date[p+1].pi)
            {
                ans+=date[p].pi+date[t].pi;
                date[p].vi=-date[p].vi;date[t].vi=-date[t].vi;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }
        }else
        {
            if(date[p].pi>date[p+1].pi)
            {
                ans+=date[p].pi;
                date[p].vi=-1;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }

        }
        }
    if(v==1)
    {
        for(i=1;i<n;i++)
            if(date[i].vi==-1)t1=i;
         for(i=n-1;i>=0;i--)
            if(date[i].vi==2)t2=i;
    if(date[t1].pi<date[t2].pi)
    {ans-=date[t1].pi-date[t2].pi;
     date[t1].vi=-date[t1].vi;
     date[t2].vi=-date[t2].vi;
     }
    }
        printf("%d\n",ans);
    for(i=0;i<n;i++)
        if(date[i].vi<0)
            printf("%d ",date[i].i);

    return 0;
}
/*5 3
1 9      9                     5 4
2 9     4.5                 3 1 5
1 9     9
2 10    5
1 6     6*/


#include<stdio.h>
#include<stdlib.h>
#include <algorithm>
using namespace  std;
typedef struct stu
 {
     int vi;
     int pi;
     float ave;
     int i;
 }student;
student date[1100000];
int n,v,sumv=0,ans=0;

int Compare(student a, student b)
{
  //  return (
            if(b.ave<a.ave)return 1 ;
            return 0;
}
int main()
{   int i,p,t=0;
    scanf("%d %d",&n,&v);
    for(i=0;i<n;i++)
    {
        scanf("%d %d",&date[i].vi,&date[i].pi);
        date[i].ave=(int)date[i].pi/date[i].vi;
        date[i].i=i+1;
        sumv+=date[i].vi;
    }
    if(sumv<v) v=sumv;
    sort(date,date+n,Compare);
    p=0;
 //   for(i=0;i<n;i++)printf("%f   ",date[i].ave);
    while((!(v==2&&date[p].vi==1&&date[p+1].vi==2))&&v>0)
    {
        if(v-date[p].vi>=0)
        {
          v-=date[p].vi;
          date[p].vi=-21;
            ans+=date[p].pi;
        }
    p++;
    }
    if(v==2)
    {
        t=-2;
        for(i=p+1;i<n;i++)
        if(date[i].vi==1){t=i;break;}
        if(t>=0)
        {
            if(date[p].pi+date[t].pi>date[p+1].pi)
            {
                ans+=date[p].pi+date[t].pi;
                date[p].vi=-1;date[t].vi=-1;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }
        }else
        {
            if(date[p].pi>date[p+1].pi)
            {
                ans+=date[p].pi;
                date[p].vi=-1;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }

        }
        }

    printf("%d\n",ans);
    for(i=0;i<n;i++)
        if(date[i].vi<0)
            printf("%d ",date[i].i);

    return 0;
}
/*5 3
1 9      9                     5 4
2 9     4.5                 3 1 5
1 9     9
2 10    5
1 6     6*/


#include<stdio.h>
#include<stdlib.h>
#include <algorithm>
using namespace  std;
typedef struct stu
 {
     float vi;
     float pi;
     float ave;
     int i;
 }student;
student date[110000];
int n,v,sumv=0,ans=0;

int Compare(student a, student b)
{
    return (b.ave-a.ave);
}
int main()
{   int i,p,t=0;

    scanf("%d %d",&n,&v);
    for(i=0;i<n;i++)
    {
        scanf("%f %f",&date[i].vi,&date[i].pi);
        date[i].ave=date[i].pi/date[i].vi;
        date[i].i=i;
        sumv+=date[i].vi;
    }
    if(sumv<v) v=sumv;
    sort(date,date+n,Compare);
    p=0;
    while((!(v==2&&date[p].vi==1&&date[p+1].vi==2))&&v>0)
    {
        if(v-date[p].vi>=0)
        {
          v-=date[p].vi;
          date[p].vi=-21;
            ans+=date[p].pi;
        }
    p++;
    }
    if(v==2)
    {
        t=-2;
        for(i=p+1;i<n;i++)
        if(date[i].vi==1){t=i;break;}
        if(t>=0)
        {
            if(date[p].pi+date[t].pi>date[p+1].pi)
            {
                ans+=date[p].pi+date[t].pi;
                date[p].vi=-1;date[t].vi=-1;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }
        }else
        {
            if(date[p].pi>date[p+1].pi)
            {
                ans+=date[p].pi;
                date[p].vi=-1;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }

        }
        }

    printf("%d ",ans);
    for(i=0;i<n;i++)
        if(date[i].vi<0)
            printf("%d",date[i].i+1);

    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include <algorithm>
using namespace  std;
typedef struct stu
 {
     int vi;
     int pi;
     float ave;
     int i;
 }student;
student date[110000];
int n,v,sumv=0,ans=0;

int Compare(student a, student b)
{
  //  return (
            if(b.ave<a.ave)return 1 ;
            return 0;
}
int main()
{   int i,p,t=0,t1,t2;
    scanf("%d %d",&n,&v);
    for(i=0;i<n;i++)
    {
        scanf("%d %d",&date[i].vi,&date[i].pi);
        date[i].ave=(float)date[i].pi/date[i].vi;
        date[i].i=i+1;
        sumv+=date[i].vi;
    }
    if(sumv<v) v=sumv;
    sort(date,date+n,Compare);
    p=0;
    //for(i=0;i<n;i++)printf("%d  %d    %f   \n",date[i].vi,date[i].pi,date[i].ave);
    while((!(v==2&&date[p].vi==1&&date[p+1].vi==2))&&v>0&&p<n)
    {
        if(v-date[p].vi>=0)
        {
          v-=date[p].vi;
          date[p].vi=- date[p].vi;
            ans+=date[p].pi;
        }
    p++;
    }
    if(v==2)
    {//printf("dsaf");
        t=-2;
        for(i=p+1;i<n;i++)
        if(date[i].vi==1){t=i;break;}
        if(t>=0)
        {
            if(date[p].pi+date[t].pi>date[p+1].pi)
            {
                ans+=date[p].pi+date[t].pi;
                date[p].vi=-date[p].vi;date[t].vi=-date[t].vi;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }
        }else
        {
            if(date[p].pi>date[p+1].pi)
            {
                ans+=date[p].pi;
                date[p].vi=-1;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }

        }
        }
    if(v==1)
    {t1=-1;t2=-1;
        for(i=1;i<n;i++)
            if(date[i].vi==-1)t1=i;
         for(i=n-1;i>=0;i--)
            if(date[i].vi==2)t2=i;
    if(date[t1].pi<date[t2].pi&&t1>=0&&t2>=0)
    {ans-=date[t1].pi-date[t2].pi;
     date[t1].vi=-date[t1].vi;
     date[t2].vi=-date[t2].vi;
     }
    }
        printf("%d\n",ans);
    for(i=0;i<n;i++)
        if(date[i].vi<0)
            printf("%d ",date[i].i);

    return 0;
}
/*5 3
1 9      9                     5 4
2 9     4.5                 3 1 5
1 9     9
2 10    5
1 6     6*/


#include<stdio.h>
#include<stdlib.h>
#include <algorithm>
using namespace  std;
typedef struct stu
 {
     int vi;
     int pi;
     float ave;
     int i;
 }student;
student date[110000];
int n,v,sumv=0,ans=0;

int Compare(student a, student b)
{
  //  return (
            if(b.ave<a.ave)return 1 ;
            return 0;
}
int main()
{   int i,p,t=0,t1,t2;
    scanf("%d %d",&n,&v);
    for(i=0;i<n;i++)
    {
        scanf("%d %d",&date[i].vi,&date[i].pi);
        date[i].ave=(float)date[i].pi/date[i].vi;
        date[i].i=i+1;
        sumv+=date[i].vi;
    }
    if(sumv<v) v=sumv;
    sort(date,date+n,Compare);
    p=0;
    //for(i=0;i<n;i++)printf("%d  %d    %f   \n",date[i].vi,date[i].pi,date[i].ave);
    while((!(v==2&&date[p].vi==1&&date[p+1].vi==2))&&v>0&&p<n)
    {
        if(v-date[p].vi>=0)
        {
          v-=date[p].vi;
          date[p].vi=- date[p].vi;
            ans+=date[p].pi;
        }
    p++;
    }
    if(v==2)
    {//printf("dsaf");
        t=-2;
        for(i=p+1;i<n;i++)
        if(date[i].vi==1){t=i;break;}
        if(t>=0)
        {
            if(date[p].pi+date[t].pi>date[p+1].pi)
            {
                ans+=date[p].pi+date[t].pi;
                date[p].vi=-date[p].vi;date[t].vi=-date[t].vi;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }
        }else
        {
            if(date[p].pi>date[p+1].pi)
            {
                ans+=date[p].pi;
                date[p].vi=-1;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }

        }
        }
 /*   if(v==1)
    {t1=-1;t2=-1;
        for(i=1;i<n;i++)
            if(date[i].vi==-1)t1=i;
         for(i=n-1;i>=0;i--)
            if(date[i].vi==2)t2=i;
    if(date[t1].pi<date[t2].pi&&t1>=0&&t2>=0)
    {ans-=date[t1].pi-date[t2].pi;
     date[t1].vi=-date[t1].vi;
     date[t2].vi=-date[t2].vi;
     }
    }*/
        printf("%d\n",ans);
    for(i=0;i<n;i++)
        if(date[i].vi<0)
            printf("%d ",date[i].i);

    return 0;
}
/*5 3
1 9      9                     5 4
2 9     4.5                 3 1 5
1 9     9
2 10    5
1 6     6*/


#include<stdio.h>
#include<stdlib.h>
#include <algorithm>
using namespace  std;
typedef struct stu
 {
     int vi;
     int pi;
     float ave;
     int i;
 }student;
student date[110000];
int n,v,sumv=0,ans=0;

int Compare(student a, student b)
{
  //  return (
            if(b.ave<a.ave)return 1 ;
            return 0;
}
int main()
{   int i,p,t,t1,t2;
    scanf("%d %d",&n,&v);
    t=n;
    date[n].pi=0;date[n].vi=0;
    for(i=0;i<n;i++)
    {
        scanf("%d %d",&date[i].vi,&date[i].pi);
        date[i].ave=(float)date[i].pi/date[i].vi;
        date[i].i=i+1;
        sumv+=date[i].vi;
    }
    if(sumv<v) v=sumv;
    sort(date,date+n,Compare);
    p=0;
    //for(i=0;i<n;i++)printf("%d  %d    %f   \n",date[i].vi,date[i].pi,date[i].ave);
    while((!(v==2&&date[p].vi==1&&date[p+1].vi==2))&&v>0&&p<n)
    {
        if(v-date[p].vi>=0)
        {
          v-=date[p].vi;
          date[p].vi=- date[p].vi;
            ans+=date[p].pi;
        }
    p++;
    }
    if(v==2)
    {//printf("dsaf");
        t=-2;
        for(i=p+1;i<n;i++)
        if(date[i].vi==1){t=i;break;}
        if(t>=0)
        {
            if(date[p].pi+date[t].pi>date[p+1].pi)
            {
                ans+=date[p].pi+date[t].pi;
                date[p].vi=-date[p].vi;date[t].vi=-date[t].vi;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }
        }else
        {
            if(date[p].pi>date[p+1].pi)
            {
                ans+=date[p].pi;
                date[p].vi=-1;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }

        }
        }
 /*   if(v==1)
    {t1=-1;t2=-1;
        for(i=1;i<n;i++)
            if(date[i].vi==-1)t1=i;
         for(i=n-1;i>=0;i--)
            if(date[i].vi==2)t2=i;
    if(date[t1].pi<date[t2].pi&&t1>=0&&t2>=0)
    {ans-=date[t1].pi-date[t2].pi;
     date[t1].vi=-date[t1].vi;
     date[t2].vi=-date[t2].vi;
     }
    }*/
        printf("%d\n",ans);
    for(i=0;i<n;i++)
        if(date[i].vi<0)
            printf("%d ",date[i].i);

    return 0;
}
/*5 3
1 9      9                     5 4
2 9     4.5                 3 1 5
1 9     9
2 10    5
1 6     6*/


#include<stdio.h>
#include<stdlib.h>
#include <algorithm>
using namespace  std;
typedef struct stu
 {
     int vi;
     int pi;
     float ave;
     int i;
 }student;
student date[110000];
int n,v,sumv=0,ans=0;

int Compare(student a, student b)
{
    return (b.ave<a.ave);
}
int main()
{   int i,p,t=0;
    scanf("%d %d",&n,&v);
    for(i=0;i<n;i++)
    {
        scanf("%d %d",&date[i].vi,&date[i].pi);
        date[i].ave=(int)date[i].pi/date[i].vi;
        date[i].i=i+1;
        sumv+=date[i].vi;
    }
    if(sumv<v) v=sumv;
    sort(date,date+n,Compare);
    p=0;
 //   for(i=0;i<n;i++)printf("%f   ",date[i].ave);
    while((!(v==2&&date[p].vi==1&&date[p+1].vi==2))&&v>0)
    {
        if(v-date[p].vi>=0)
        {
          v-=date[p].vi;
          date[p].vi=-21;
            ans+=date[p].pi;
        }
    p++;
    }
    if(v==2)
    {
        t=-2;
        for(i=p+1;i<n;i++)
        if(date[i].vi==1){t=i;break;}
        if(t>=0)
        {
            if(date[p].pi+date[t].pi>date[p+1].pi)
            {
                ans+=date[p].pi+date[t].pi;
                date[p].vi=-1;date[t].vi=-1;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }
        }else
        {
            if(date[p].pi>date[p+1].pi)
            {
                ans+=date[p].pi;
                date[p].vi=-1;
            } else
            {
                 ans+=date[p+1].pi;
                date[p+1].vi=-1;
            }

        }
        }

    printf("%d\n",ans);
    for(i=0;i<n;i++)
        if(date[i].vi<0)
            printf("%d ",date[i].i);

    return 0;
}
/*5 3
1 9      9                     5 4
2 9     4.5                 3 1 5
1 9     9
2 10    5
1 6     6*/


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <cstdio>

using namespace std;

#define PB push_back
#define MP make_pair

//#define DEBUG

struct str
{
       long long gruz;
       int num;
};

bool operator < ( const str & i, const str & j )
{
     return i.gruz < j.gruz;
}

int main()
{
#ifdef DEBUG
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
#endif
   long N, V, type, vv;
   cin >> N >> V;
   str st;
   vector < str > list_1, list_2;
   list_1.reserve(N);
   list_2.reserve(N);
   for ( long Q = 0; Q < N; ++Q )
   {
      cin >> type >> st.gruz;
      st.num = Q+1;
      if ( type == 1 )
         list_1.PB(st);
      else
         list_2.PB(st);
   }

   sort(list_1.begin(), list_1.end()), sort(list_2.begin(), list_2.end());
   vector < int > res;
   res.reserve(N);
   long long result = 0;

   while ( V > 0 && ( list_1.size() > 1 || list_2.size() > 1 ) )
   {
         if ( list_1.size () == 0 )
         {
              if ( V >= 2 )
                 result += list_2.back().gruz, res.PB(list_2.back().num), V -= 2;
              else
                 V = 0;
              list_2.pop_back();
              continue;
         }
         if ( list_2.size() == 0 )
         {
              if ( V >= 1 )
                 result += list_1.back().gruz, res.PB(list_1.back().num), V -= 1;
              else
                 V = 0;
              list_1.pop_back();
              continue;
         }
         if ( list_1.size() == 1 )
         {
              if ( V <= 1 )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 1;
                   list_1.pop_back();
                   continue;
              }

              if ( list_1.back().gruz > list_2.back().gruz )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 2;
                   list_1.pop_back();
                   continue;
              }
              else
              {
                   result += list_2.back().gruz;
                   res.PB(list_2.back().num);
                   V -= 2;
                   list_2.pop_back();
                   continue;
              }
         }
         else
         {
              if ( V & 1 == 1 )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 1;
                   list_1.pop_back();
                   continue;
              }
              if ( list_1.back().gruz + list_1[list_1.size() - 2].gruz > list_2.back().gruz )
              {
                   result += list_1.back().gruz + list_1[list_1.size() - 2].gruz;
                   res.PB(list_1.back().num), res.PB(list_1[list_1.size() - 2].num);
                   V -= 2;
                   list_1.pop_back(), list_1.pop_back();
                   continue;
              }
              else
              {
                   result += list_2.back().gruz;
                   res.PB(list_2.back().num);
                   list_2.pop_back();
                   V -= 2;
                   continue;
              }
         }
   }
   cout << result << endl;
   for ( long Q = 0; Q < res.size(); ++Q )
     cout << res[Q] << " ";
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <cstdio>

using namespace std;

#define PB push_back
#define MP make_pair

#define DEBUG

struct str
{
 	   long long gruz;
 	   int num;
};

bool operator < ( const str & i, const str & j )
{
 	 return i.gruz < j.gruz;
}

int main()
{
#ifdef DEBUG
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
#endif
   long N, V, type, vv;
   cin >> N >> V;
   str st;
   vector < str > list_1, list_2;
   list_1.reserve(N);
   list_2.reserve(N);
   for ( long Q = 0; Q < N; ++Q )
   {
      cin >> type >> st.gruz;
      st.num = Q+1;
      if ( type == 1 )
         list_1.PB(st);
      else
	     list_2.PB(st);
   }

   sort(list_1.begin(), list_1.end()), sort(list_2.begin(), list_2.end());
   vector < int > res;
   res.reserve(N);
   long long result = 0;

   while ( V > 0 && ( list_1.size() > 1 || list_2.size() > 1 ) )
   {
   		 if ( list_1.size () == 0 )
   		 {
		  	  if ( V >= 2 )
		  	     result += list_2.back().gruz, res.PB(list_2.back().num), V -= 2;
		  	  else
		  	     V = 0;
		  	  list_2.pop_back();
		  	  continue;
		 }
		 if ( list_2.size() == 0 )
		 {
		  	  if ( V >= 1 )
		  	     result += list_1.back().gruz, res.PB(list_1.back().num), V -= 1;
		  	  else
		  	     V = 0;
		  	  list_1.pop_back();
		  	  continue;
		 }
		 if ( list_1.size() == 1 )
		 {
		  	  if ( V <= 1 )
		  	  {
			   	   result += list_1.back().gruz;
			   	   res.PB(list_1.back().num);
			   	   V -= 1;
			   	   list_1.pop_back();
			   	   continue;
			  }

			  if ( list_1.back().gruz > list_2.back().gruz )
			  {
			   	   result += list_1.back().gruz;
			   	   res.PB(list_1.back().num);
			   	   V -= 2;
			   	   list_1.pop_back();
			   	   continue;
			  }
			  else
			  {
			   	   result += list_2.back().gruz;
			   	   res.PB(list_2.back().num);
			   	   V -= 2;
			   	   list_2.pop_back();
			   	   continue;
			  }
	     }
	     else
	     {
		  	  if ( V == 1 )
		  	  {
			   	   result += list_1.back().gruz;
			   	   res.PB(list_1.back().num);
			   	   V -= 1;
			   	   list_1.pop_back();
			   	   continue;
			  }
			  if ( list_1.back().gruz + list_1[list_1.size() - 2].gruz > list_2.back().gruz )
			  {
			   	   result += list_1.back().gruz;
			   	   res.PB(list_1.back().num);
			   	   V -= 1;
			   	   list_1.pop_back();
			   	   continue;
			  }
			  else
			  {
			   	   result += list_2.back().gruz;
			   	   res.PB(list_2.back().num);
			   	   list_2.pop_back();
			   	   V -= 2;
			   	   continue;
			  }
		 }
   }
   cout << result << endl;
   for ( long Q = 0; Q < res.size(); ++Q )
     cout << res[Q] << " ";
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <cstdio>

using namespace std;

#define PB push_back
#define MP make_pair

//#define DEBUG

struct str
{
       long long gruz;
       int num;
};

bool operator < ( const str & i, const str & j )
{
     return i.gruz < j.gruz;
}

int main()
{
#ifdef DEBUG
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
#endif
   long N, V, type, vv;
   cin >> N >> V;
   str st;
   vector < str > list_1, list_2;
   list_1.reserve(N);
   list_2.reserve(N);
   for ( long Q = 0; Q < N; ++Q )
   {
      cin >> type >> st.gruz;
      st.num = Q+1;
      if ( type == 1 )
         list_1.PB(st);
      else
         list_2.PB(st);
   }

   sort(list_1.begin(), list_1.end()), sort(list_2.begin(), list_2.end());
   vector < int > res;
   res.reserve(N);
   long long result = 0;

   while ( V > 0 && ( list_1.size() > 1 || list_2.size() > 1 ) )
   {
         if ( list_1.size () == 0 )
         {
              if ( V >= 2 )
                 result += list_2.back().gruz, res.PB(list_2.back().num), V -= 2;
              else
                 V = 0;
              list_2.pop_back();
              continue;
         }
         if ( list_2.size() == 0 )
         {
              if ( V >= 1 )
                 result += list_1.back().gruz, res.PB(list_1.back().num), V -= 1;
              else
                 V = 0;
              list_1.pop_back();
              continue;
         }
         if ( list_1.size() == 1 )
         {
              if ( V <= 1 )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 1;
                   list_1.pop_back();
                   continue;
              }

              if ( list_1.back().gruz > list_2.back().gruz )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 2;
                   list_1.pop_back();
                   continue;
              }
              else
              {
                   result += list_2.back().gruz;
                   res.PB(list_2.back().num);
                   V -= 2;
                   list_2.pop_back();
                   continue;
              }
         }
         else
         {
              if ( V & 1 == 1 )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 1;
                   list_1.pop_back();
                   continue;
              }
              if ( list_1.back().gruz + list_1[list_1.size() - 2].gruz > list_2.back().gruz )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 1;
                   list_1.pop_back();
                   continue;
              }
              else
              {
                   result += list_2.back().gruz;
                   res.PB(list_2.back().num);
                   list_2.pop_back();
                   V -= 2;
                   continue;
              }
         }
   }
   cout << result << endl;
   for ( long Q = 0; Q < res.size(); ++Q )
     cout << res[Q] << " ";
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <cstdio>

using namespace std;

#define PB push_back
#define MP make_pair

//#define DEBUG

struct str
{
       long long gruz;
       int num;
};

bool operator < ( const str & i, const str & j )
{
     return i.gruz < j.gruz;
}

int main()
{
#ifdef DEBUG
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
#endif
   long N, V, type, vv;
   cin >> N >> V;
   str st;
   vector < str > list_1, list_2;
   list_1.reserve(N);
   list_2.reserve(N);
   for ( long Q = 0; Q < N; ++Q )
   {
      cin >> type >> st.gruz;
      st.num = Q+1;
      if ( type == 1 )
         list_1.PB(st);
      else
         list_2.PB(st);
   }

   sort(list_1.begin(), list_1.end()), sort(list_2.begin(), list_2.end());
   vector < int > res;
   res.reserve(N);
   long long result = 0;

   while ( V > 0 && ( list_1.size() > 1 || list_2.size() > 1 ) )
   {
         if ( list_1.size () == 0 )
         {
              if ( V >= 2 )
                 result += list_2.back().gruz, res.PB(list_2.back().num), V -= 2;
              else
                 V = 0;
              list_2.pop_back();
              continue;
         }
         if ( list_2.size() == 0 )
         {
              if ( V >= 1 )
                 result += list_1.back().gruz, res.PB(list_1.back().num), V -= 1;
              else
                 V = 0;
              list_1.pop_back();
              continue;
         }
         if ( list_1.size() == 1 )
         {
              if ( V <= 1 )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 1;
                   list_1.pop_back();
                   continue;
              }

              if ( list_1.back().gruz > list_2.back().gruz )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 2;
                   list_1.pop_back();
                   continue;
              }
              else
              {
                   result += list_2.back().gruz;
                   res.PB(list_2.back().num);
                   V -= 2;
                   list_2.pop_back();
                   continue;
              }
         }
         else
         {
              if ( V == 1 )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 1;
                   list_1.pop_back();
                   continue;
              }
              if ( list_1.back().gruz + list_1[list_1.size() - 2].gruz > list_2.back().gruz )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 2;
                   list_1.pop_back();
                   continue;
              }
              else
              {
                   result += list_2.back().gruz;
                   res.PB(list_2.back().num);
                   list_2.pop_back();
                   V -= 2;
                   continue;
              }
         }
   }
   cout << result << endl;
   for ( long Q = 0; Q < res.size(); ++Q )
     cout << res[Q] << " ";
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <cstdio>

using namespace std;

#define PB push_back
#define MP make_pair

//#define DEBUG

struct str
{
       long long gruz;
       int num;
};

bool operator < ( const str & i, const str & j )
{
     return i.gruz < j.gruz;
}

int main()
{
#ifdef DEBUG
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
#endif
   long N, V, type, vv;
   cin >> N >> V;
   str st;
   vector < str > list_1, list_2;
   list_1.reserve(N);
   list_2.reserve(N);
   for ( long Q = 0; Q < N; ++Q )
   {
      cin >> type >> st.gruz;
      st.num = Q+1;
      if ( type == 1 )
         list_1.PB(st);
      else
         list_2.PB(st);
   }

   sort(list_1.begin(), list_1.end()), sort(list_2.begin(), list_2.end());
   vector < int > res;
   res.reserve(N);
   long long result = 0;

   while ( V > 0 && ( list_1.size() > 1 || list_2.size() > 1 ) )
   {
         if ( list_1.size () == 0 )
         {
              if ( V >= 2 )
                 result += list_2.back().gruz, res.PB(list_2.back().num), V -= 2;
              else
                 V = 0;
              list_2.pop_back();
              continue;
         }
         if ( list_2.size() == 0 )
         {
              if ( V >= 1 )
                 result += list_1.back().gruz, res.PB(list_1.back().num), V -= 1;
              else
                 V = 0;
              list_1.pop_back();
              continue;
         }
         if ( list_1.size() == 1 )
         {
              if ( V & 1 == 1 )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 1;
                   list_1.pop_back();
                   continue;
              }

              if ( list_1.back().gruz > list_2.back().gruz )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 1;
                   list_1.pop_back();
                   continue;
              }
              else
              {
                   result += list_2.back().gruz;
                   res.PB(list_2.back().num);
                   V -= 2;
                   list_2.pop_back();
                   continue;
              }
         }
         else
         {
              if ( V & 1 == 1 )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 1;
                   list_1.pop_back();
                   continue;
              }
              if ( list_1.back().gruz + list_1[list_1.size() - 2].gruz > list_2.back().gruz )
              {
                   result += list_1.back().gruz + list_1[list_1.size() - 2].gruz;
                   res.PB(list_1.back().num), res.PB(list_1[list_1.size() - 2].num);
                   V -= 2;
                   list_1.pop_back(), list_1.pop_back();
                   continue;
              }
              else
              {
                   result += list_2.back().gruz;
                   res.PB(list_2.back().num);
                   list_2.pop_back();
                   V -= 2;
                   continue;
              }
         }
   }
   cout << result << endl;
   for ( long Q = 0; Q < res.size(); ++Q )
     cout << res[Q] << " ";
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <cstdio>

using namespace std;

#define PB push_back
#define MP make_pair

//#define DEBUG

struct str
{
 	   long long gruz;
 	   int num;
};

bool operator < ( const str & i, const str & j )
{
 	 return i.gruz < j.gruz;
}

int main()
{
#ifdef DEBUG
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
#endif
   long N, V, type, vv;
   cin >> N >> V;
   str st;
   vector < str > list_1, list_2;
   list_1.reserve(N);
   list_2.reserve(N);
   for ( long Q = 0; Q < N; ++Q )
   {
      cin >> type >> st.gruz;
      st.num = Q+1;
      if ( type == 1 )
         list_1.PB(st);
      else
	     list_2.PB(st);
   }

   sort(list_1.begin(), list_1.end()), sort(list_2.begin(), list_2.end());
   vector < int > res;
   res.reserve(N);
   long long result = 0;

   while ( V > 0 && list_1.size() > 1 || list_2.size() > 1 )
   {
   		 if ( list_1.size () == 0 )
   		 {
		  	  if ( V >= 2 )
		  	     result += list_2.back().gruz, res.PB(list_2.back().num), V -= 2;
		  	  else
		  	     V = 0;
		  	  list_2.pop_back();
		  	  continue;
		 }
		 if ( list_2.size() == 0 )
		 {
		  	  if ( V >= 1 )
		  	     result += list_1.back().gruz, res.PB(list_1.back().num), V -= 1;
		  	  else
		  	     V = 0;
		  	  list_1.pop_back();
		  	  continue;
		 }
		 if ( list_1.size() == 1 )
		 {
		  	  if ( V <= 1 )
		  	  {
			   	   result += list_1.back().gruz;
			   	   res.PB(list_1.back().num);
			   	   V -= 1;
			   	   list_1.pop_back();
			   	   continue;
			  }

			  if ( list_1.back().gruz > list_2.back().gruz )
			  {
			   	   result += list_1.back().gruz;
			   	   res.PB(list_1.back().num);
			   	   V -= 2;
			   	   list_1.pop_back();
			   	   continue;
			  }
			  else
			  {
			   	   result += list_2.back().gruz;
			   	   res.PB(list_2.back().num);
			   	   V -= 2;
			   	   list_2.pop_back();
			   	   continue;
			  }
	     }
	     else
	     {
		  	  if ( V == 1 )
		  	  {
			   	   result += list_1.back().gruz;
			   	   res.PB(list_1.back().num);
			   	   V -= 1;
			   	   list_1.pop_back();
			   	   continue;
			  }
			  if ( list_1.back().gruz + list_1[list_1.size() - 2].gruz > list_2.back().gruz )
			  {
			   	   result += list_1.back().gruz + list_1[list_1.size() - 2].gruz;
			   	   res.PB(list_1.back().num), res.PB(list_1[list_1.size() - 2].num);
			   	   V -= 2;
			   	   list_1.pop_back(), list_1.pop_back();
			   	   continue;
			  }
			  else
			  {
			   	   result += list_2.back().gruz;
			   	   res.PB(list_2.back().num);
			   	   list_2.pop_back();
			   	   V -= 2;
			   	   continue;
			  }
		 }
   }
   cout << result << endl;
   for ( long Q = 0; Q < res.size(); ++Q )
     cout << res[Q] << " ";
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <cstdio>

using namespace std;

#define PB push_back
#define MP make_pair

//#define DEBUG

struct str
{
 	   long long gruz;
 	   int num;
};

bool operator < ( const str & i, const str & j )
{
 	 return i.gruz < j.gruz;
}

int main()
{
#ifdef DEBUG
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
#endif
   long N, V, type, vv;
   cin >> N >> V;
   str st;
   vector < str > list_1, list_2;
   list_1.reserve(N);
   list_2.reserve(N);
   for ( long Q = 0; Q < N; ++Q )
   {
      cin >> type >> st.gruz;
      st.num = Q+1;
      if ( type == 1 )
         list_1.PB(st);
      else
	     list_2.PB(st);
   }

   sort(list_1.begin(), list_1.end()), sort(list_2.begin(), list_2.end());
   vector < int > res;
   res.reserve(N);
   long long result = 0;

   while ( V > 0 && ( list_1.size() > 1 || list_2.size() > 1 ) )
   {
   		 if ( list_1.size () == 0 )
   		 {
		  	  if ( V >= 2 )
		  	     result += list_2.back().gruz, res.PB(list_2.back().num), V -= 2;
		  	  else
		  	     V = 0;
		  	  list_2.pop_back();
		  	  continue;
		 }
		 if ( list_2.size() == 0 )
		 {
		  	  if ( V >= 1 )
		  	     result += list_1.back().gruz, res.PB(list_1.back().num), V -= 1;
		  	  else
		  	     V = 0;
		  	  list_1.pop_back();
		  	  continue;
		 }
		 if ( list_1.size() == 1 )
		 {
		  	  if ( V <= 1 )
		  	  {
			   	   result += list_1.back().gruz;
			   	   res.PB(list_1.back().num);
			   	   V -= 1;
			   	   list_1.pop_back();
			   	   continue;
			  }

			  if ( list_1.back().gruz > list_2.back().gruz )
			  {
			   	   result += list_1.back().gruz;
			   	   res.PB(list_1.back().num);
			   	   V -= 2;
			   	   list_1.pop_back();
			   	   continue;
			  }
			  else
			  {
			   	   result += list_2.back().gruz;
			   	   res.PB(list_2.back().num);
			   	   V -= 2;
			   	   list_2.pop_back();
			   	   continue;
			  }
	     }
	     else
	     {
		  	  if ( V == 1 )
		  	  {
			   	   result += list_1.back().gruz;
			   	   res.PB(list_1.back().num);
			   	   V -= 1;
			   	   list_1.pop_back();
			   	   continue;
			  }
			  if ( list_1.back().gruz + list_1[list_1.size() - 2].gruz > list_2.back().gruz )
			  {
			   	   result += list_1.back().gruz;
			   	   res.PB(list_1.back().num);
			   	   V -= 1;
			   	   list_1.pop_back();
			   	   continue;
			  }
			  else
			  {
			   	   result += list_2.back().gruz;
			   	   res.PB(list_2.back().num);
			   	   list_2.pop_back();
			   	   V -= 2;
			   	   continue;
			  }
		 }
   }
   cout << result << endl;
   for ( long Q = 0; Q < res.size(); ++Q )
     cout << res[Q] << " ";
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <cstdio>

using namespace std;

#define PB push_back
#define MP make_pair

#define DEBUG

struct str
{
       long long gruz;
       int num;
};

bool operator < ( const str & i, const str & j )
{
     return i.gruz < j.gruz;
}

int main()
{
#ifdef DEBUG
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
#endif
   long N, V, type, vv;
   cin >> N >> V;
   str st;
   vector < str > list_1, list_2;
   list_1.reserve(N);
   list_2.reserve(N);
   for ( long Q = 0; Q < N; ++Q )
   {
      cin >> type >> st.gruz;
      st.num = Q+1;
      if ( type == 1 )
         list_1.PB(st);
      else
         list_2.PB(st);
   }

   sort(list_1.begin(), list_1.end()), sort(list_2.begin(), list_2.end());
   vector < int > res;
   res.reserve(N);
   long long result = 0;

   while ( V > 0 && ( list_1.size() > 1 || list_2.size() > 1 ) )
   {
         if ( list_1.size () == 0 )
         {
              if ( V >= 2 )
                 result += list_2.back().gruz, res.PB(list_2.back().num), V -= 2;
              else
                 V = 0;
              list_2.pop_back();
              continue;
         }
         if ( list_2.size() == 0 )
         {
              if ( V >= 1 )
                 result += list_1.back().gruz, res.PB(list_1.back().num), V -= 1;
              else
                 V = 0;
              list_1.pop_back();
              continue;
         }
         if ( list_1.size() == 1 )
         {
              if ( V <= 1 )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 1;
                   list_1.pop_back();
                   continue;
              }

              if ( list_1.back().gruz > list_2.back().gruz )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 2;
                   list_1.pop_back();
                   continue;
              }
              else
              {
                   result += list_2.back().gruz;
                   res.PB(list_2.back().num);
                   V -= 2;
                   list_2.pop_back();
                   continue;
              }
         }
         else
         {
              if ( V == 1 )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 1;
                   list_1.pop_back();
                   continue;
              }
              if ( list_1.back().gruz + list_1[list_1.size() - 2].gruz > list_2.back().gruz )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 2;
                   list_1.pop_back();
                   continue;
              }
              else
              {
                   result += list_2.back().gruz;
                   res.PB(list_2.back().num);
                   list_2.pop_back();
                   V -= 2;
                   continue;
              }
         }
   }
   cout << result << endl;
   for ( long Q = 0; Q < res.size(); ++Q )
     cout << res[Q] << " ";
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <cstdio>

using namespace std;

#define PB push_back
#define MP make_pair

//#define DEBUG

struct str
{
       long long gruz;
       int num;
};

bool operator < ( const str & i, const str & j )
{
     return i.gruz < j.gruz;
}

int main()
{
#ifdef DEBUG
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
#endif
   long N, V, type, vv;
   cin >> N >> V;
   str st;
   vector < str > list_1, list_2;
   list_1.reserve(N);
   list_2.reserve(N);
   for ( long Q = 0; Q < N; ++Q )
   {
      cin >> type >> st.gruz;
      st.num = Q+1;
      if ( type == 1 )
         list_1.PB(st);
      else
         list_2.PB(st);
   }

   sort(list_1.begin(), list_1.end()), sort(list_2.begin(), list_2.end());
   vector < int > res;
   res.reserve(N);
   long long result = 0;

   while ( V > 0 && ( list_1.size() >= 1 || list_2.size() >= 1 ) )
   {
         if ( list_1.size () == 0 )
         {
              if ( V >= 2 )
                 result += list_2.back().gruz, res.PB(list_2.back().num), V -= 2;
              else
                 V = 0;
              list_2.pop_back();
              continue;
         }
         if ( list_2.size() == 0 )
         {
              if ( V >= 1 )
                 result += list_1.back().gruz, res.PB(list_1.back().num), V -= 1;
              else
                 V = 0;
              list_1.pop_back();
              continue;
         }
         if ( list_1.size() == 1 )
         {
              if ( V & 1 == 1 )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 1;
                   list_1.pop_back();
                   continue;
              }

              if ( list_1.back().gruz > list_2.back().gruz )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 1;
                   list_1.pop_back();
                   continue;
              }
              else
              {
                   result += list_2.back().gruz;
                   res.PB(list_2.back().num);
                   V -= 2;
                   list_2.pop_back();
                   continue;
              }
         }
         else
         {
              if ( V & 1 == 1 )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 1;
                   list_1.pop_back();
                   continue;
              }
              if ( list_1.back().gruz + list_1[list_1.size() - 2].gruz > list_2.back().gruz )
              {
                   result += list_1.back().gruz + list_1[list_1.size() - 2].gruz;
                   res.PB(list_1.back().num), res.PB(list_1[list_1.size() - 2].num);
                   V -= 2;
                   list_1.pop_back(), list_1.pop_back();
                   continue;
              }
              else
              {
                   result += list_2.back().gruz;
                   res.PB(list_2.back().num);
                   list_2.pop_back();
                   V -= 2;
                   continue;
              }
         }
   }
   cout << result << endl;
   for ( long Q = 0; Q < res.size(); ++Q )
     cout << res[Q] << " ";
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <cstdio>

using namespace std;

#define PB push_back
#define MP make_pair

//#define DEBUG

struct str
{
 	   long long gruz;
 	   int num;
};

bool operator < ( const str & i, const str & j )
{
 	 return i.gruz < j.gruz;
}

int main()
{
#ifdef DEBUG
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
#endif
   long N, V, type, vv;
   cin >> N >> V;
   str st;
   vector < str > list_1, list_2;
   list_1.reserve(N);
   list_2.reserve(N);
   for ( long Q = 0; Q < N; ++Q )
   {
      cin >> type >> st.gruz;
      st.num = Q+1;
      if ( type == 1 )
         list_1.PB(st);
      else
	     list_2.PB(st);
   }

   sort(list_1.begin(), list_1.end()), sort(list_2.begin(), list_2.end());
   vector < int > res;
   res.reserve(N);
   long long result = 0;

   while ( V > 0 && ( list_1.size() > 1 || list_2.size() > 1 ) )
   {
   		 if ( list_1.size () == 0 )
   		 {
		  	  if ( V >= 2 )
		  	     result += list_2.back().gruz, res.PB(list_2.back().num), V -= 2;
		  	  else
		  	     V = 0;
		  	  list_2.pop_back();
		  	  continue;
		 }
		 if ( list_2.size() == 0 )
		 {
		  	  if ( V >= 1 )
		  	     result += list_1.back().gruz, res.PB(list_1.back().num), V -= 1;
		  	  else
		  	     V = 0;
		  	  list_1.pop_back();
		  	  continue;
		 }
		 if ( list_1.size() == 1 )
		 {
		  	  if ( V <= 1 )
		  	  {
			   	   result += list_1.back().gruz;
			   	   res.PB(list_1.back().num);
			   	   V -= 1;
			   	   list_1.pop_back();
			   	   continue;
			  }

			  if ( list_1.back().gruz > list_2.back().gruz )
			  {
			   	   result += list_1.back().gruz;
			   	   res.PB(list_1.back().num);
			   	   V -= 2;
			   	   list_1.pop_back();
			   	   continue;
			  }
			  else
			  {
			   	   result += list_2.back().gruz;
			   	   res.PB(list_2.back().num);
			   	   V -= 2;
			   	   list_2.pop_back();
			   	   continue;
			  }
	     }
	     else
	     {
		  	  if ( V == 1 )
		  	  {
			   	   result += list_1.back().gruz;
			   	   res.PB(list_1.back().num);
			   	   V -= 1;
			   	   list_1.pop_back();
			   	   continue;
			  }
			  if ( list_1.back().gruz + list_1[list_1.size() - 2].gruz > list_2.back().gruz )
			  {
			   	   result += list_1.back().gruz + list_1[list_1.size() - 2].gruz;
			   	   res.PB(list_1.back().num), res.PB(list_1[list_1.size() - 2].num);
			   	   V -= 2;
			   	   list_1.pop_back(), list_1.pop_back();
			   	   continue;
			  }
			  else
			  {
			   	   result += list_2.back().gruz;
			   	   res.PB(list_2.back().num);
			   	   list_2.pop_back();
			   	   V -= 2;
			   	   continue;
			  }
		 }
   }
   cout << result << endl;
   for ( long Q = 0; Q < res.size(); ++Q )
     cout << res[Q] << " ";
}