#include <bits/stdc++.h>
using namespace std;
int n , s ;
int t , v ;
int t1s,t2s;
pair<int,int> t1[100100],t2[100100];
int sum,ans[100100];
int minns,indmin;
int main()
{
    cin >> n >> s ;
    for(int i=0,j=0;i+j<n;)
    {
        cin >> t >> v ;
        if(t==1)
        {
            t1[i].first=v;
            t1[i].second=i+j;
            i++;
        }
        else
        {
            t2[j].first=v;
            t2[j].second=i+j;
            j++;
        }
        t1s=i;
        t2s=j;
    }
    sort(t1,t1+t1s);
    sort(t2,t2+t2s);
    int s1,i1=t1s-1,j1=t2s-1,ai=0;
    while(s>1)
    {
        //cout << " f" ;
        if(i1>=0 && j1>=0)
        {
            if(i1>0)
            {
                s1=t1[i1].first+t1[i1-1].first;

            }
            else
            {
                s1=t1[i1].first;

            }
            //cout << s1 << "          " << t2[j1].first <<endl;
           // cout << t1[i1].second+1 <<":"<<t1[i1-1].second+1<<"         "<<t2[j1].second+1<<endl<<endl;
            if(s1>t2[j1].first)
            {
                //cout << "1" << endl;
                if(i1>0)
                {sum+=s1;
                s-=2;
                minns=t1[i1-1].first;
                ans[ai++]=t1[i1].second;
                indmin=ai;
                ans[ai++]=t1[i1-1].second;
                i1-=2;}
                else
                {
                    sum+=s1;
                    s--;
                    indmin=ai;
                    minns=t1[i1].first;
                    ans[ai++]=t1[i1].second;
                    i1--;

                }
            }
            else
            {
                //cout << "2" <<endl;
                sum+=t2[j1].first;
                s-=2;
                ans[ai++]=t2[j1].second;
                j1--;
            }
        }
        else if(j1>=0)
        {
            //cout << "3" <<endl;
            sum+=t2[j1].first;
            s-=2;
            ans[ai++]=t2[j1].second;
            j1--;

        }
        else if(i1>=0)
        {
            //cout << "4" <<endl;
            sum+=t1[i1].first;
            s--;
            ans[ai++]=t1[i1].second;
            i1--;
        }
        else break;
    }
    if(s==1)
    {
        if(minns < t2[j1].first && j1>=0)
        {
            sum+= t2[j1].first - minns;
            ans[indmin]=t2[j1].second;
            s--;
        }
        else if(i1>=0)
        {
             sum+=t1[i1].first;
             ans[ai++]=t1[i1].second;
             s--;
             i1--;
        }
    }
    //cout << s << endl;
    cout << sum << endl;
    for(int i=0;i<ai;i++)
    {
        cout << ans[i]+1 <<" ";
    }
    cout << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n , s ;
int t , v ;
int t1s,t2s;
pair<int,int> t1[100100],t2[100100];
int sum,ans[100100];
int minns,indmin;
int main()
{
    cin >> n >> s ;
    for(int i=0,j=0;i+j<n;)
    {
        cin >> t >> v ;
        if(t==1)
        {
            t1[i].first=v;
            t1[i].second=i+j;
            i++;
        }
        else
        {
            t2[j].first=v;
            t2[j].second=i+j;
            j++;
        }
        t1s=i;
        t2s=j;
    }
    sort(t1,t1+t1s);
    sort(t2,t2+t2s);
    int s1,i1=t1s-1,j1=t2s-1,ai=0;
    while(s>1)
    {
        //cout << " f" ;
        if(i1>=0 && j1>=0)
        {
            if(i1>0)
            {
                s1=t1[i1].first+t1[i1-1].first;

            }
            else
            {
                s1=t1[i1].first;

            }
            //cout << s1 << "          " << t2[j1].first <<endl;
            //cout << t1[i1].second+1 <<":"<<t1[i1-1].second+1<<"         "<<t2[j1].second+1<<endl<<endl;
            if(s1>t2[j1].first)
            {
                //cout << "1" << endl;
                if(i1>0)
                {sum+=s1;
                s-=2;
                minns=t1[i1-1].first;
                ans[ai++]=t1[i1].second;
                indmin=ai;
                ans[ai++]=t1[i1-1].second;
                i1-=2;}
                else
                {
                    sum+=s1;
                    s--;
                    indmin=ai;
                    minns=t1[i1].first;
                    ans[ai++]=t1[i1].second;
                    i1--;

                }
            }
            else
            {
                //cout << "2" <<endl;
                sum+=t2[j1].first;
                s-=2;
                ans[ai++]=t2[j1].second;
                j1--;
            }
        }
        else if(j1>=0)
        {
            //cout << "3" <<endl;
            sum+=t2[j1].first;
            s-=2;
            ans[ai++]=t2[j1].second;
            j1--;

        }
        else if(i1>=0)
        {
            //cout << "4" <<endl;
            sum+=t1[i1].first;
            s--;
            ans[ai++]=t1[i1].second;
            i1--;
        }
        else break;
    }
//    if(s==1 && i1>=0)
//    {
//        sum+=t1[i1].first;
//        ans[ai++]=t1[i1].second;
//        s--;
//        i1--;
//    }
    if(s==1)
    {
        if(i1>=0)
        {
        if(minns+t1[i1].first < t2[j1].first && j1>=0)
        {
            sum+= t2[j1].first - minns;
            ans[indmin]=t2[j1].second;
            s--;
        }
        else
        {
             sum+=t1[i1].first;
             ans[ai++]=t1[i1].second;
             s--;
             i1--;
        }
        }
        else
        {
            if(minns < t2[j1].first && j1>=0)
            {
            sum+= t2[j1].first - minns;
            ans[indmin]=t2[j1].second;
            s--;
            }
        }
    }
    //cout << s << endl;
    cout << sum << endl;
    for(int i=0;i<ai;i++)
    {
        cout << ans[i]+1 <<" ";
    }
    cout << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n , s ;
int t , v ;
int t1s,t2s;
pair<int,int> t1[100100],t2[100100];
int sum,ans[100100];
int main()
{
    cin >> n >> s ;
    for(int i=0,j=0;i+j<n;)
    {
        cin >> t >> v ;
        if(t==1)
        {
            t1[i].first=v;
            t1[i].second=i+j;
            i++;
        }
        else
        {
            t2[j].first=v;
            t2[j].second=i+j;
            j++;
        }
        t1s=i;
        t2s=j;
    }
    sort(t1,t1+t1s);
    sort(t2,t2+t2s);
    int s1,i1=t1s-1,j1=t2s-1,ai=0;
    while(s>1)
    {
        //cout << " f" ;
        if(i1>0 && j1>=0)
        {
            s1=t1[i1].first+t1[i1-1].first;
            if(s1>t2[j1].first)
            {
                //cout << "1" ;
                sum+=s1;
                s-=2;
                ans[ai++]=t1[i1].second;
                ans[ai++]=t1[i1-1].second;
                i1-=2;
            }
            else
            {
                //cout << "2" ;
                sum+=t2[j1].first;
                s-=2;
                ans[ai++]=t2[j1].second;
                j1--;
            }
        }
        else if(j1>=0)
        {
            //cout << "3" ;
            sum+=t2[j1].first;
            s-=2;
            ans[ai++]=t2[j1].second;
            j1--;

        }
        else if(i1>=0)
        {
            //cout << "4" ;
            sum+=t1[i1].first;
            s--;
            ans[ai++]=t1[i1].second;
            i1--;
        }
        else break;
    }
    if(s==1 && i1>=0)
    {
        sum+=t1[i1].first;
        ans[ai++]=t1[i1].second;
        s--;
        i1--;
    }
    sort(ans,ans+ai);
    cout << sum << endl;
    for(int i=0;i<ai;i++)
    {
        cout << ans[i]+1 <<" ";
    }
    cout << endl;

    return 0;
}


/*
5 4
1 2
1 2
1 4
1 5
2 3

13
4 3 1 2

4 3
2 10
1 8
1 7
1 6

21
2 3 4

*/
#include <iostream>
#include <algorithm>
using namespace std;
struct boat{
	int cap;
	int type;
	int no;
}b[100001];
bool cmp(boat a,boat c){
	if(a.type==c.type) return a.cap>c.cap;
	return  a.type<c.type;
}
int main(){
   int n,v,i,k,j;
   cin>>n>>v;
   int out[n]={0};
   for(i=0;i<n;i++){
   	cin>>b[i].type>>b[i].cap;
   	b[i].no=i+1;
   }
   sort(b,b+n,cmp);
   int pos=0;
   while(b[pos].type==1){
   	 pos++;
   }
 //  for(i=0;i<n;i++)
 //  cout<<b[i].no<<" "<<b[i].cap<<" "<<b[i].type<<endl;
   int rest=v;
   i=0;
   k=0;
   int sum=0;
   int temp[n];
   while(rest>0&&i<n){
   	for(i;i<n;i++){
   		if(rest>=b[i].type){
   			out[k]=b[i].no;
   			temp[k]=i;
   			sum+=b[i].cap;
   			k++;
   			rest-=b[i].type;
   			i++;
   			break;
		   }
	   }
	      }
	//    cout<<"k: "<<k<<" i: "<<i<<" pos: "<<pos<<endl;
   j=k-1;
   i=k;
   while(b[temp[j]].type==2){
   	 j--;
   }
   if(b[i].type!=2) i=pos;
   if(rest>0){
   	sum+=b[i].cap-b[temp[j]].cap;
   	out[j]=b[i].no;
   	i++;
   	j--;
   }
   int geshu=k;
   for(j;j>=0&&i<n;j=j-2){
   	if(b[temp[j]].cap+b[temp[j-1]].cap<b[i].cap){
   		sum+=b[i].cap-(b[temp[j]].cap+b[temp[j-1]].cap);
   		//cout<<"b[i].no:"<<b[i].no<<endl;
   		k=k-2;
   		if(out[k+2]!=0){
   			out[k]=out[k+2];
		out[k+1]=b[i].no;
		   }
   		else{
   			out[k]=b[i].no;
		   }
   		geshu--;
   		i++;
	   }
	   else
	   break;
   }
   cout<<sum<<endl;
   for(int i=0;i<geshu;i++){
   	if(i==0) cout<<out[0];
   	else cout<<" "<<out[i];
   }
   cout<<"\n";
    return 0;
}


/*
5 4
1 2
1 2
1 4
1 5
2 3

13
4 3 1 2

4 3
2 10
1 8
1 7
1 6

21
2 3 4

*/
#include <iostream>
#include <algorithm>
using namespace std;
struct boat{
	int cap;
	int type;
	int no;
}b[100001];
bool cmp(boat a,boat c){
	if(a.type==c.type) return a.cap>c.cap;
	return  a.type<c.type;
}
int main(){
   int n,v,i,k,j,k2;
   cin>>n>>v;
   int out[n]={0};
   int out2[n]={0};
   for(i=0;i<n;i++){
   	cin>>b[i].type>>b[i].cap;
   	b[i].no=i+1;
   }
   sort(b,b+n,cmp);
   int pos=0;
   while(b[pos].type==1){
   	 pos++;
   }
 //  for(i=0;i<n;i++)
//  cout<<b[i].no<<" "<<b[i].cap<<" "<<b[i].type<<endl;
   int rest=v;
   i=0;
   k=0;
   k2=0;
   int k1=0;
   int sum=0;
   int temp[n];
   while(rest>0&&i<n){
   	for(i;i<n;i++){
   		if(rest>=b[i].type){
   			if(b[i].type==1)
   		{
   				out[k1]=b[i].no;
   				k1++;
			   }
   			else
   			{out2[k2]=b[i].no;
   				k2++;
			   }
   			temp[k]=i;
   			sum+=b[i].cap;
   			k++;
   			rest-=b[i].type;
   			i++;
   			break;
		   }
	   }
	      }
	//    cout<<"k: "<<k<<" i: "<<i<<" pos: "<<pos<<endl;
   j=k-1;
   i=k;
   while(b[temp[j]].type==2){
   	 j--;
   }
   if(b[i].type!=2) i=pos;
   if(rest>0){
   	sum+=b[i].cap-b[temp[j]].cap;
   	k1--;
   	out2[k2++]=b[i].no;
   	i++;
   	j--;
   }
   int geshu=k;
   int time=0;
   int tmp=i;
   for(j;j>=0&&i<n;j=j-2){
   	if(b[temp[j]].cap+b[temp[j-1]].cap<b[i].cap){
   		time++;
   		sum+=b[i].cap-(b[temp[j]].cap+b[temp[j-1]].cap);
   //	cout<<"b[i].no:"<<b[i].no<<endl;
   	k1=k1-2;
   	out2[k2++]=b[i].no;
   	//	for(int t=0;t<time;t++){
   		//	if(t==time-1) out[k-time*2+t]=b[i].no;
   		//	else out[k-time*2+t]=out[k-(time-1)*2+t];
		//   }
		   	i++;
	}
   	 else
	   break;
	   }
	  // for(int t=0;t<time;t++){
	   	//out[k-2*time+t]=b[tmp+t].no;
	   //}
   cout<<sum<<endl;
    for(int i=0;i<k1;i++){
   //	if(i==0) cout<<out[0];
    cout<<out[i]<<" ";
   }
   for(int i=0;i<k2;i++){
   	//if(i==0) cout<<out2[0];
   	 cout<<out2[i]<<" ";
   }
   //cout<<"\n";
    return 0;
}


/*
5 4
1 2
1 2
1 4
1 5
2 3

13
4 3 1 2

4 3
2 10
1 8
1 7
1 6

21
2 3 4

*/
#include <iostream>
#include <algorithm>
using namespace std;
struct boat{
	int cap;
	int type;
	int no;
}b[100001];
bool cmp(boat a,boat c){
	if(a.type==c.type) return a.cap>c.cap;
	return  a.type<c.type;
}
int main(){
   int n,v,i,k,j,k2;
   cin>>n>>v;
   int out[n]={0};
   int out2[n]={0};
   for(i=0;i<n;i++){
   	cin>>b[i].type>>b[i].cap;
   	b[i].no=i+1;
   }
   sort(b,b+n,cmp);
   int pos=0;
   while(b[pos].type==1){
   	 pos++;
   }
 //  for(i=0;i<n;i++)
//  cout<<b[i].no<<" "<<b[i].cap<<" "<<b[i].type<<endl;
   int rest=v;
   i=0;
   k=0;
   k2=0;
   int k1=0;
   int sum=0;
   int temp[n];
   while(rest>0&&i<n){
   	for(i;i<n;i++){
   		if(rest>=b[i].type){
   			if(b[i].type==1)
   		{
   				out[k1]=b[i].no;
   				k1++;
			   }
   			else
   			{out2[k2]=b[i].no;
   				k2++;
			   }
   			temp[k]=i;
   			sum+=b[i].cap;
   			k++;
   			rest-=b[i].type;
   			i++;
   			break;
		   }
	   }
	      }
	//    cout<<"k: "<<k<<" i: "<<i<<" pos: "<<pos<<endl;
   j=k-1;
   i=k;
   while(b[temp[j]].type==2){
   	 j--;
   }
   if(b[i-1].type!=2) i=pos;
   if(rest>0){
   	sum+=b[i].cap-b[temp[j]].cap;
   	k1--;
   	out2[k2++]=b[i].no;
   	i++;
   	j--;
   }
   int geshu=k;
   int tmp=i;
   for(j;j>=0&&j-1>=0&&i<n;j=j-2){
   	if(b[temp[j]].cap+b[temp[j-1]].cap<b[i].cap){
   		sum+=b[i].cap-(b[temp[j]].cap+b[temp[j-1]].cap);
   //	cout<<"b[i].no:"<<b[i].no<<endl;
   	k1=k1-2;
   	out2[k2++]=b[i].no;
   	//	for(int t=0;t<time;t++){
   		//	if(t==time-1) out[k-time*2+t]=b[i].no;
   		//	else out[k-time*2+t]=out[k-(time-1)*2+t];
		//   }
		   	i++;
	}
   	 else
	   break;
	   }
	  // for(int t=0;t<time;t++){
	   	//out[k-2*time+t]=b[tmp+t].no;
	   //}
   cout<<sum<<endl;
    for(int i=0;i<k1;i++){
   //	if(i==0) cout<<out[0];
    cout<<out[i]<<" ";
   }
   for(int i=0;i<k2;i++){
   	//if(i==0) cout<<out2[0];
   	 cout<<out2[i]<<" ";
   }
   //cout<<"\n";
    return 0;
}


/*
5 4
1 2
1 2
1 4
1 5
2 3

13
4 3 1 2

4 3
2 10
1 8
1 7
1 6

21
2 3 4

*/
#include <iostream>
#include <algorithm>
using namespace std;
struct boat{
	int cap;
	int type;
	int no;
}b[100001];
bool cmp(boat a,boat c){
	if(a.type==c.type) return a.cap>c.cap;
	return  a.type<c.type;
}
int main(){
   int n,v,i,k,j;
   cin>>n>>v;
   int out[n]={0};
   for(i=0;i<n;i++){
   	cin>>b[i].type>>b[i].cap;
   	b[i].no=i+1;
   }
   sort(b,b+n,cmp);
  // for(i=0;i<n;i++)
 //  cout<<b[i].no<<" "<<b[i].cap<<" "<<b[i].type<<endl;
   int rest=v;
   i=0;
   k=0;
   int sum=0;
   int temp[n];
   while(rest>0){
   	for(i;i<n;i++){
   		if(rest>=b[i].type){
   			out[k]=b[i].no;
   			temp[k]=i;
   			sum+=b[i].cap;
   			k++;
   			rest-=b[i].type;
   			i++;
   			break;
		   }
	   }
   }
   j=k-1;
   while(b[temp[j]].type==2){
   	 j--;
   }
   for(j;j>=0&&i<n;j=j-2){
   	if(b[temp[j]].cap+b[temp[j-1]].cap<b[i].cap){
   		sum+=b[i].cap-(b[temp[j]].cap+b[temp[j-1]].cap);
   		out[k-2]=i;
   		k--;
   		i++;
	   }
   }
   cout<<sum<<endl;
   for(int i=0;i<k;i++){
   	if(i==0) cout<<out[0];
   	else cout<<" "<<out[i];
   }
   cout<<"\n";
    return 0;
}


/*
5 4
1 2
1 2
1 4
1 5
2 3

13
4 3 1 2

4 3
2 10
1 8
1 7
1 6

21
2 3 4

*/
#include <iostream>
#include <algorithm>
using namespace std;
struct boat{
	int cap;
	int type;
	int no;
}b[100001];
bool cmp(boat a,boat c){
	if(a.type==c.type) return a.cap>c.cap;
	return  a.type<c.type;
}
int main(){
   int n,v,i,k,j;
   cin>>n>>v;
   int out[n]={0};
   for(i=0;i<n;i++){
   	cin>>b[i].type>>b[i].cap;
   	b[i].no=i+1;
   }
   sort(b,b+n,cmp);
  // for(i=0;i<n;i++)
   //cout<<b[i].no<<" "<<b[i].cap<<" "<<b[i].type<<endl;
   int rest=v;
   i=0;
   k=0;
   int sum=0;
   int temp[n];
   while(rest>0&&i<n){
   	for(i;i<n;i++){
   		if(rest>=b[i].type){
   			out[k]=b[i].no;
   			temp[k]=i;
   			sum+=b[i].cap;
   			k++;
   			rest-=b[i].type;
   			i++;
   			break;
		   }
	   }
	      }
	 //   cout<<"k: "<<k<<" i: "<<i<<endl;
   j=k-1;
   i=k;
   while(b[temp[j]].type==2){
   	 j--;
   }
   if(rest>0){
   	sum+=b[i].cap-b[temp[j]].cap;
   	out[j]=b[i].no;
   	i++;
   	j--;
   }
   for(j;j>=0&&i<n;j=j-2){
   	if(b[temp[j]].cap+b[temp[j-1]].cap<b[i].cap){
   		sum+=b[i].cap-(b[temp[j]].cap+b[temp[j-1]].cap);
   		out[k-2]=i;
   		k--;
   		i++;
	   }
	   else
	   break;
   }
   cout<<sum<<endl;
   for(int i=0;i<k;i++){
   	if(i==0) cout<<out[0];
   	else cout<<" "<<out[i];
   }
   cout<<"\n";
    return 0;
}


/*
5 4
1 2
1 2
1 4
1 5
2 3

13
4 3 1 2

4 3
2 10
1 8
1 7
1 6

21
2 3 4

*/
#include <iostream>
#include <algorithm>
using namespace std;
struct boat{
	int cap;
	int type;
	int no;
}b[100001];
bool cmp(boat a,boat c){
	if(a.type==c.type) return a.cap>c.cap;
	return  a.type<c.type;
}
int main(){
   int n,v,i,k,j;
   cin>>n>>v;
   int out[n]={0};
   for(i=0;i<n;i++){
   	cin>>b[i].type>>b[i].cap;
   	b[i].no=i+1;
   }
   sort(b,b+n,cmp);
  // for(i=0;i<n;i++)
 //  cout<<b[i].no<<" "<<b[i].cap<<" "<<b[i].type<<endl;
   int rest=v;
   i=0;
   k=0;
   int sum=0;
   int temp[n];
   while(rest>0){
   	for(i;i<n;i++){
   		if(rest>=b[i].type){
   			out[k]=b[i].no;
   			temp[k]=i;
   			sum+=b[i].cap;
   			k++;
   			rest-=b[i].type;
   			i++;
   			break;
		   }
	   }
   }
   j=k-1;
   while(b[temp[j]].type==2){
   	 j--;
   }
   for(j;j>=0&&i<n;j=j-2){
   	if(b[temp[j]].cap+b[temp[j-1]].cap<b[i].cap){
   		sum+=b[i].cap-(b[temp[j]].cap+b[temp[j-1]].cap);
   		out[k-2]=i;
   		k--;
   		i++;
	   }
	   else
	   break;
   }
   cout<<sum<<endl;
   for(int i=0;i<k;i++){
   	if(i==0) cout<<out[0];
   	else cout<<" "<<out[i];
   }
   cout<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
pair<int,int> p1[maxn],p2[maxn];
int sum1[maxn],sum2[maxn];
int n1,n2;
bool cmp(pair<int,int> A,pair<int,int> B)
{
    return A.first>B.first;
}
int main()
{
    int n,v;
    scanf("%d%d",&n,&v);
    for(int i=1;i<=n;i++)
    {
        int x,y;scanf("%d%d",&x,&y);
        if(x==1)p1[++n1]=make_pair(y,i);
        else p2[++n2]=make_pair(y,i);
    }
    sort(p1+1,p1+1+n1,cmp);
    sort(p2+1,p2+1+n2,cmp);
    for(int i=1;i<=n1;i++)
        sum1[i]=sum1[i-1]+p1[i].first;
    for(int i=1;i<=n2;i++)
        sum2[i]=sum2[i-1]+p2[i].first;
    int Ans=0,x=0,y=0;
    for(int i=0;i<=n1;i++)
    {
        if(i>v)break;
        int l=0,r=n2,ans=0;
   int l=0,r=n2,ans=0;
        while(l<=r)
        {
            int mid = (l+r)/2;
            if(2*mid<=v-i)ans=mid,l=mid+1;
            else r=mid-1;
        }
        int tmp=sum1[i]+sum2[ans];
        if(tmp>Ans)
        {
            Ans=tmp;
            x=i,y=ans;
        }
    }
    printf("%d\n",Ans);
    for(int i=1;i<=x;i++)
        printf("%d ",p1[i].second);
    for(int i=1;i<=y;i++)
        printf("%d ",p2[i].second);
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
pair<int,int> p1[maxn],p2[maxn];
int sum1[maxn],sum2[maxn];
int n1,n2;
bool cmp(pair<int,int> A,pair<int,int> B)
{
    return A.first>B.first;
}
int main()
{
    int n,v;
    scanf("%d%d",&n,&v);
    for(int i=1;i<=n;i++)
    {
        int x,y;scanf("%d%d",&x,&y);
        if(x==1)p1[++n1]=make_pair(y,i);
        else p2[++n2]=make_pair(y,i);
    }
    sort(p1+1,p1+1+n1,cmp);
    sort(p2+1,p2+1+n2,cmp);
    for(int i=1;i<=n1;i++)
        sum1[i]=sum1[i-1]+p1[i].first;
    for(int i=1;i<=n2;i++)
        sum2[i]=sum2[i-1]+p2[i].first;
    int Ans=0,x=0,y=0;
    for(int i=0;i<=n1;i++)
    {
        if(i>v)break;
        int l=0,r=n2,ans=0;
        while(l<=r)
        {
            int mid = (l+r)/2;
            if(2*mid<=v-i)ans=mid,l=mid+1;
            else r=mid-1;
        }
        int tmp=sum1[i]+sum2[ans];
        if(tmp>Ans)
        {
            Ans=tmp;
            x=i,y=ans;
        }
    }
    printf("%d\n",Ans);
    for(int i=1;i<=x;i++)
        printf("%d ",p1[i].second);
    for(int i=1;i<=y;i++)
        printf("%d ",p2[i].second);
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
pair<int,int> p1[maxn],p2[maxn];
int sum1[maxn],sum2[maxn];
int n1,n2;
bool cmp(pair<int,int> A,pair<int,int> B)
{
    return A.first>B.first;
}
int main()
{
    int n,v;
    scanf("%d%d",&n,&v);
    for(int i=1;i<=n;i++)
    {
        int x,y;scanf("%d%d",&x,&y);
        if(x==1)p1[++n1]=make_pair(y,i);
        else p2[++n2]=make_pair(y,i);
    }
    sort(p1+1,p1+1+n1,cmp);
    sort(p2+1,p2+1+n2,cmp);
    for(int i=1;i<=n1;i++)
        sum1[i]=sum1[i-1]+p1[i].first;
    for(int i=1;i<=n2;i++)
        sum2[i]=sum2[i-1]+p2[i].first;
    int Ans=0,x=0,y=0;
    for(int i=0;i<=n1;i++)
    {
        if(i>v)break;
        int l=0,r=n2,ans=0;
  ans=(v-i)/2;
        int tmp=sum1[i]+sum2[ans];
        if(tmp>Ans)
        {
            Ans=tmp;
            x=i,y=ans;
        }
    }
    printf("%d\n",Ans);
    for(int i=1;i<=x;i++)
        printf("%d ",p1[i].second);
    for(int i=1;i<=y;i++)
        printf("%d ",p2[i].second);
}


/*
5 4
1 2
1 2
1 4
1 5
2 3

13
4 3 1 2

4 3
2 10
1 8
1 7
1 6

21
2 3 4

*/
#include <iostream>
#include <algorithm>
using namespace std;
struct boat{
	int cap;
	int type;
	int no;
}b[100001];
bool cmp(boat a,boat c){
	if(a.type==c.type) return a.cap>c.cap;
	return  a.type<c.type;
}
int main(){
   int n,v,i,k,j,k2;
   cin>>n>>v;
   int out[n]={0};
   int out2[n]={0};
   for(i=0;i<n;i++){
   	cin>>b[i].type>>b[i].cap;
   	b[i].no=i+1;
   }
   sort(b,b+n,cmp);
   int pos=0;
   while(b[pos].type==1){
   	 pos++;
   }
 //  for(i=0;i<n;i++)
//  cout<<b[i].no<<" "<<b[i].cap<<" "<<b[i].type<<endl;
   int rest=v;
   i=0;
   k=0;
   k2=0;
   int k1=0;
   int sum=0;
   int temp[n];
   while(rest>0&&i<n){
   	for(i;i<n;i++){

   		if(rest>=b[i].type){
   			if(b[i].type==1)
   		{
   				out[k1]=b[i].no;
   				k1++;
			   }
   			else
   			{out2[k2]=b[i].no;
   				k2++;
			   }
   			temp[k]=i;
   			sum+=b[i].cap;

   			k++;
   			rest-=b[i].type;
   			i++;
   			break;
		   }
	   }
	      }
		  // cout<<sum<<endl;
	  //  cout<<"k: "<<k<<" i: "<<i<<" pos: "<<pos<<endl;
   j=k-1;
   i=k;
   if(k==0){
   	cout<<"0";
   	return 0;
   }
   while(b[temp[j]].type==2){
   	 j--;
   }
   if(b[i-1].type!=2) i=pos;
   if(rest>0&&k<n){
   	sum+=b[i].cap-b[temp[j]].cap;
   	k1--;
   	out2[k2++]=b[i].no;
   	i++;
   	j--;
   }
   int tmp=i;
   for(j;j>=0&&j-1>=0&&i<n;j=j-2){

   	if(b[temp[j]].cap+b[temp[j-1]].cap<b[i].cap){
   		sum+=b[i].cap-(b[temp[j]].cap+b[temp[j-1]].cap);
   //	cout<<"b[i].no:"<<b[i].no<<endl;
   	k1=k1-2;
   	out2[k2++]=b[i].no;
   	//	for(int t=0;t<time;t++){
   		//	if(t==time-1) out[k-time*2+t]=b[i].no;
   		//	else out[k-time*2+t]=out[k-(time-1)*2+t];
		//   }
		   	i++;
	}
   	 else
	   break;
	   }
	  // for(int t=0;t<time;t++){
	   	//out[k-2*time+t]=b[tmp+t].no;
	   //}
   cout<<sum<<endl;
    for(int i=0;i<k1;i++){
   //	if(i==0) cout<<out[0];
    cout<<out[i]<<" ";
   }
   for(int i=0;i<k2;i++){
   	//if(i==0) cout<<out2[0];
   	 cout<<out2[i]<<" ";
   }
   //cout<<"\n";
    return 0;
}


/*
5 4
1 2
1 2
1 4
1 5
2 3

13
4 3 1 2

4 3
2 10
1 8
1 7
1 6

21
2 3 4

*/
#include <iostream>
#include <algorithm>
using namespace std;
struct boat{
	int cap;
	int type;
	int no;
}b[100001];
bool cmp(boat a,boat c){
	if(a.type==c.type) return a.cap>c.cap;
	return  a.type<c.type;
}
int main(){
   int n,v,i,k,j;
   cin>>n>>v;
   int out[n]={0};
   for(i=0;i<n;i++){
   	cin>>b[i].type>>b[i].cap;
   	b[i].no=i+1;
   }
   sort(b,b+n,cmp);
   int pos=0;
   while(b[pos].type==1){
   	 pos++;
   }
 //  for(i=0;i<n;i++)
 //  cout<<b[i].no<<" "<<b[i].cap<<" "<<b[i].type<<endl;
   int rest=v;
   i=0;
   k=0;
   int sum=0;
   int temp[n];
   while(rest>0&&i<n){
   	for(i;i<n;i++){
   		if(rest>=b[i].type){
   			out[k]=b[i].no;
   			temp[k]=i;
   			sum+=b[i].cap;
   			k++;
   			rest-=b[i].type;
   			i++;
   			break;
		   }
	   }
	      }
	//    cout<<"k: "<<k<<" i: "<<i<<" pos: "<<pos<<endl;
   j=k-1;
   i=k;
   while(b[temp[j]].type==2){
   	 j--;
   }
   if(b[i].type!=2) i=pos;
   if(rest>0){
   	sum+=b[i].cap-b[temp[j]].cap;
   	out[j]=b[i].no;
   	i++;
   	j--;
   }
   int geshu=k;
   int time=0;
   int tmp=i;
   for(j;j>=0&&i<n;j=j-2){
   	if(b[temp[j]].cap+b[temp[j-1]].cap<b[i].cap){
   		time++;
   		sum+=b[i].cap-(b[temp[j]].cap+b[temp[j-1]].cap);
   		//cout<<"b[i].no:"<<b[i].no<<endl;
   	//	for(int t=0;t<time;t++){
   		//	if(t==time-1) out[k-time*2+t]=b[i].no;
   		//	else out[k-time*2+t]=out[k-(time-1)*2+t];
		//   }
		   	i++;
	}
   	 else
	   break;
	   }
	   for(int t=0;t<time;t++){
	   	out[k-2*time+t]=b[tmp+t].no;
	   }
   cout<<sum<<endl;
   for(int i=0;i<k-time;i++){
   	if(i==0) cout<<out[0];
   	else cout<<" "<<out[i];
   }
   cout<<"\n";
    return 0;
}


/*
5 4
1 2
1 2
1 4
1 5
2 3

13
4 3 1 2

4 3
2 10
1 8
1 7
1 6

21
2 3 4

*/
#include <iostream>
#include <algorithm>
using namespace std;
struct boat{
	int cap;
	int type;
	int no;
}b[100001];
bool cmp(boat a,boat c){
	if(a.type==c.type) return a.cap>c.cap;
	return  a.type<c.type;
}
int main(){
   int n,v,i,k,j,k2;
   cin>>n>>v;
   int out[n]={0};
   int out2[n]={0};
   for(i=0;i<n;i++){
   	cin>>b[i].type>>b[i].cap;
   	b[i].no=i+1;
   }
   sort(b,b+n,cmp);
   int pos=0;
   while(b[pos].type==1){
   	 pos++;
   }
 //  for(i=0;i<n;i++)
//  cout<<b[i].no<<" "<<b[i].cap<<" "<<b[i].type<<endl;
   int rest=v;
   i=0;
   k=0;
   k2=0;
   int k1=0;
   int sum=0;
   int temp[n];
   while(rest>0&&i<n){
   	for(i;i<n;i++){
   		if(rest>=b[i].type){
   			if(b[i].type==1)
   		{
   				out[k1]=b[i].no;
   				k1++;
			   }
   			else
   			{out2[k2]=b[i].no;
   				k2++;
			   }
   			temp[k]=i;
   			sum+=b[i].cap;
   			k++;
   			rest-=b[i].type;
   			i++;
   			break;
		   }
	   }
	      }
	//    cout<<"k: "<<k<<" i: "<<i<<" pos: "<<pos<<endl;
   j=k-1;
   i=k;
   while(b[temp[j]].type==2){
   	 j--;
   }
   if(b[i-1].type!=2) i=pos;
   if(rest>0){
   	sum+=b[i].cap-b[temp[j]].cap;
   	k1--;
   	out2[k2++]=b[i].no;
   	i++;
   	j--;
   }
   int geshu=k;
   int time=0;
   int tmp=i;
   for(j;j>=0&&i<n;j=j-2){
   	if(b[temp[j]].cap+b[temp[j-1]].cap<b[i].cap){
   		time++;
   		sum+=b[i].cap-(b[temp[j]].cap+b[temp[j-1]].cap);
   //	cout<<"b[i].no:"<<b[i].no<<endl;
   	k1=k1-2;
   	out2[k2++]=b[i].no;
   	//	for(int t=0;t<time;t++){
   		//	if(t==time-1) out[k-time*2+t]=b[i].no;
   		//	else out[k-time*2+t]=out[k-(time-1)*2+t];
		//   }
		   	i++;
	}
   	 else
	   break;
	   }
	  // for(int t=0;t<time;t++){
	   	//out[k-2*time+t]=b[tmp+t].no;
	   //}
   cout<<sum<<endl;
    for(int i=0;i<k1;i++){
   //	if(i==0) cout<<out[0];
    cout<<out[i]<<" ";
   }
   for(int i=0;i<k2;i++){
   	//if(i==0) cout<<out2[0];
   	 cout<<out2[i]<<" ";
   }
   //cout<<"\n";
    return 0;
}


/*
5 4
1 2
1 2
1 4
1 5
2 3

13
4 3 1 2

4 3
2 10
1 8
1 7
1 6

21
2 3 4

*/
#include <iostream>
#include <algorithm>
using namespace std;
struct boat{
	int cap;
	int type;
	int no;
}b[100001];
bool cmp(boat a,boat c){//按照代价小的包裹优先，相同代价的价值优先
	if(a.type==c.type) return a.cap>c.cap;
	return  a.type<c.type;
}
int main(){
   int n,v,i,k,j,k2;
   cin>>n>>v;
   int out[n]={0};//存放代价为1的包裹
   int out2[n]={0};//存放代价为2的包裹
   for(i=0;i<n;i++){
   	cin>>b[i].type>>b[i].cap;
   	b[i].no=i+1;
   }
   sort(b,b+n,cmp);
   int pos=0;//代价为2的物品在序列中的第几个
   while(b[pos].type==1){
   	 pos++;
   }
   int rest=v;
   i=0;
   k=0;//放入包裹的物品数量
   k2=0;//放入包裹的代价为2的数量
   int k1=0;//放入包裹的代价为1的数量
   int sum=0;
   int temp[n];//放入包裹的物品在序列中的位置
   while(rest>0&&i<n){
   	for(i;i<n;i++){//查看序列中能放入包裹的物品，逐一加入 ；每放入一个就要退出循环判断剩余的容量
   		if(rest>=b[i].type){//包裹有剩余容量
   			if(b[i].type==1)
   		{
   				out[k1]=b[i].no;
   				k1++;
			   }
   			else
   			{out2[k2]=b[i].no;
   				k2++;
			   }
   			temp[k]=i;
   			sum+=b[i].cap;

   			k++;
   			rest-=b[i].type;
   			i++;
   			break;
		   }
	   }
	      }
   j=k-1;
   i=k;
   if(k==0){//如果包裹容量不足以放入任何一个物品的情况
   	cout<<"0";
   	return 0;
   }
   while(b[temp[j]].type==2){ //找到放入包裹中的代价为1的最后一个包裹的位置
   	 j--;
   }
   if(b[i-1].type!=2) i=pos;//判断如果要更改为代价为2的包裹，最先被放入的包裹在序列中的位置
   if(rest>0&&k<n){//如果包裹有剩余且不是所有物品都放入包裹则说明可能可以放入一个代价为2的物品去掉一个代价为1的物品，前提是这个代价为2的物品的价值比代价为1的高
   	if(b[i].cap>b[temp[j]].cap){
   		sum+=b[i].cap-b[temp[j]].cap;
   	k1--;
   	out2[k2++]=b[i].no;
   	i++;
   	j--;
	   }
   }
   for(j;j>=0&&j-1>=0&&i<n;j=j-2){

   	if(b[temp[j]].cap+b[temp[j-1]].cap<b[i].cap){//如果代价为2的物品的价值大等于两个代价为1的物品的价值则把包裹中的两个为1的换成为2的
   		sum+=b[i].cap-(b[temp[j]].cap+b[temp[j-1]].cap);
   	k1=k1-2;
   	out2[k2++]=b[i].no;//把物品序号进行修改
		   	i++;
	}
   	 else//如果以上情况不成立那么包裹就不存在更高的价值
	   break;
	   }
   cout<<sum<<endl;
    for(int i=0;i<k1;i++){
    cout<<out[i]<<" ";
   }
   for(int i=0;i<k2;i++){
   	 cout<<out2[i]<<" ";
   }
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
struct boat{
	int cap;
	int type;
	int no;
}b[100001];
bool cmp(boat a,boat c){
	if(a.cap==c.cap) return a.type<c.type;
	return  a.cap>c.cap;
}
int main(){
   int n,v,i,k;
   cin>>n>>v;
   int out[n]={0};
   for(i=0;i<n;i++){
   	cin>>b[i].type>>b[i].cap;
   	b[i].no=i+1;
   }
   sort(b,b+n,cmp);
  // for(i=0;i<n;i++)
 //  cout<<b[i].no<<" "<<b[i].cap<<" "<<b[i].type<<endl;
   int rest=v;
   i=0;
   k=0;
   int sum=0;
   while(rest>0){
   	for(i;i<n;i++){
   	//	cout<<"i: "<<i<<"type: "<<b[i].type<<endl;
   		if(rest>=b[i].type){
   			out[k]=b[i].no;
   			sum+=b[i].cap;
   			k++;
   			rest-=b[i].type;
   			break;
		   }
	   }
   }
   cout<<sum<<endl;
   for(int i=0;i<k;i++){
   	if(i==0) cout<<out[0];
   	else cout<<" "<<out[i];
   }
   cout<<"\n";
    return 0;
}


/*
5 4
1 2
1 2
1 4
1 5
2 3

13
4 3 1 2

4 3
2 10
1 8
1 7
1 6

21
2 3 4

*/
#include <iostream>
#include <algorithm>
using namespace std;
struct boat{
	int cap;
	int type;
	int no;
}b[100001];
bool cmp(boat a,boat c){
	if(a.type==c.type) return a.cap>c.cap;
	return  a.type<c.type;
}
int main(){
   int n,v,i,k,j,k2;
   cin>>n>>v;
   int out[n]={0};
   int out2[n]={0};
   for(i=0;i<n;i++){
   	cin>>b[i].type>>b[i].cap;
   	b[i].no=i+1;
   }
   sort(b,b+n,cmp);
   int pos=0;
   while(b[pos].type==1){
   	 pos++;
   }
 //  for(i=0;i<n;i++)
//  cout<<b[i].no<<" "<<b[i].cap<<" "<<b[i].type<<endl;
   int rest=v;
   i=0;
   k=0;
   k2=0;
   int k1=0;
   int sum=0;
   int temp[n];
   while(rest>0&&i<n){
   	for(i;i<n;i++){
   		if(rest>=b[i].type){
   			if(b[i].type==1)
   		{
   				out[k1]=b[i].no;
   				k1++;
			   }
   			else
   			{out2[k2]=b[i].no;
   				k2++;
			   }
   			temp[k]=i;
   			sum+=b[i].cap;
   			k++;
   			rest-=b[i].type;
   			i++;
   			break;
		   }
	   }
	      }
	//    cout<<"k: "<<k<<" i: "<<i<<" pos: "<<pos<<endl;
   j=k-1;
   i=k;
   while(b[temp[j]].type==2){
   	 j--;
   }
   if(b[i].type!=2) i=pos;
   if(rest>0){
   	sum+=b[i].cap-b[temp[j]].cap;
   	k1--;
   	out2[++k2]=b[i].no;
   	i++;
   	j--;
   }
   int geshu=k;
   int time=0;
   int tmp=i;
   for(j;j>=0&&i<n;j=j-2){
   	if(b[temp[j]].cap+b[temp[j-1]].cap<b[i].cap){
   		time++;
   		sum+=b[i].cap-(b[temp[j]].cap+b[temp[j-1]].cap);
   //	cout<<"b[i].no:"<<b[i].no<<endl;
   	k1=k1-2;
   	out2[k2++]=b[i].no;
   	//	for(int t=0;t<time;t++){
   		//	if(t==time-1) out[k-time*2+t]=b[i].no;
   		//	else out[k-time*2+t]=out[k-(time-1)*2+t];
		//   }
		   	i++;
	}
   	 else
	   break;
	   }
	  // for(int t=0;t<time;t++){
	   	//out[k-2*time+t]=b[tmp+t].no;
	   //}
   cout<<sum<<endl;
    for(int i=0;i<k1;i++){
   //	if(i==0) cout<<out[0];
    cout<<out[i]<<" ";
   }
   for(int i=0;i<k2;i++){
   	//if(i==0) cout<<out2[0];
   	 cout<<out2[i]<<" ";
   }
   //cout<<"\n";
    return 0;
}


/*
5 4
1 2
1 2
1 4
1 5
2 3

13
4 3 1 2

4 3
2 10
1 8
1 7
1 6

21
2 3 4

*/
#include <iostream>
#include <algorithm>
using namespace std;
struct boat{
	int cap;
	int type;
	int no;
}b[100001];
bool cmp(boat a,boat c){
	if(a.type==c.type) return a.cap>c.cap;
	return  a.type<c.type;
}
int main(){
   int n,v,i,k,j;
   cin>>n>>v;
   int out[n]={0};
   for(i=0;i<n;i++){
   	cin>>b[i].type>>b[i].cap;
   	b[i].no=i+1;
   }
   sort(b,b+n,cmp);
   int pos=0;
   while(b[pos].type==1){
   	 pos++;
   }
 //  for(i=0;i<n;i++)
 //  cout<<b[i].no<<" "<<b[i].cap<<" "<<b[i].type<<endl;
   int rest=v;
   i=0;
   k=0;
   int sum=0;
   int temp[n];
   while(rest>0&&i<n){
   	for(i;i<n;i++){
   		if(rest>=b[i].type){
   			out[k]=b[i].no;
   			temp[k]=i;
   			sum+=b[i].cap;
   			k++;
   			rest-=b[i].type;
   			i++;
   			break;
		   }
	   }
	      }
	  //  cout<<"k: "<<k<<" i: "<<i<<" pos: "<<pos<<endl;
   j=k-1;
   i=k;
   while(b[temp[j]].type==2){
   	 j--;
   }
   if(b[k].type!=2) i=pos;
   if(rest>0){
   	sum+=b[i].cap-b[temp[j]].cap;
   	out[j]=b[i].no;
   	i++;
   	j--;
   }
   for(j;j>=0&&i<n;j=j-2){
   	if(b[temp[j]].cap+b[temp[j-1]].cap<b[i].cap){
   		sum+=b[i].cap-(b[temp[j]].cap+b[temp[j-1]].cap);
   		out[k-2]=i;
   		k--;
   		i++;
	   }
	   else
	   break;
   }
   cout<<sum<<endl;
   for(int i=0;i<k;i++){
   	if(i==0) cout<<out[0];
   	else cout<<" "<<out[i];
   }
   cout<<"\n";
    return 0;
}


/*
5 4
1 2
1 2
1 4
1 5
2 3

13
4 3 1 2

4 3
2 10
1 8
1 7
1 6

21
2 3 4

*/
#include <iostream>
#include <algorithm>
using namespace std;
struct boat{
	int cap;
	int type;
	int no;
}b[100001];
bool cmp(boat a,boat c){
	if(a.type==c.type) return a.cap>c.cap;
	return  a.type<c.type;
}
int main(){
   int n,v,i,k,j;
   cin>>n>>v;
   int out[n]={0};
   for(i=0;i<n;i++){
   	cin>>b[i].type>>b[i].cap;
   	b[i].no=i+1;
   }
   sort(b,b+n,cmp);
   int pos=0;
   while(b[pos].type==1){
   	 pos++;
   }
 //  for(i=0;i<n;i++)
 //  cout<<b[i].no<<" "<<b[i].cap<<" "<<b[i].type<<endl;
   int rest=v;
   i=0;
   k=0;
   int sum=0;
   int temp[n];
   while(rest>0&&i<n){
   	for(i;i<n;i++){
   		if(rest>=b[i].type){
   			out[k]=b[i].no;
   			temp[k]=i;
   			sum+=b[i].cap;
   			k++;
   			rest-=b[i].type;
   			i++;
   			break;
		   }
	   }
	      }
	//    cout<<"k: "<<k<<" i: "<<i<<" pos: "<<pos<<endl;
   j=k-1;
   i=k;
   while(b[temp[j]].type==2){
   	 j--;
   }
   if(b[i].type!=2) i=pos;
   if(rest>0){
   	sum+=b[i].cap-b[temp[j]].cap;
   	out[j]=b[i].no;
   	i++;
   	j--;
   }
   int geshu=k;
   int time=0;
   for(j;j>=0&&i<n;j=j-2){
   	if(b[temp[j]].cap+b[temp[j-1]].cap<b[i].cap){
   		time++;
   		sum+=b[i].cap-(b[temp[j]].cap+b[temp[j-1]].cap);
   		//cout<<"b[i].no:"<<b[i].no<<endl;
   		for(int t=0;t<time;t++){
   			if(t==time-1) out[k-time*2+t]=b[i].no;
   			else out[k-time*2+t]=out[k-(time-1)*2+t];
		   }
   		i++;
	   }
	   else
	   break;
   }
   cout<<sum<<endl;
   for(int i=0;i<k-time;i++){
   	if(i==0) cout<<out[0];
   	else cout<<" "<<out[i];
   }
   cout<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
typedef long long ll;
int f[N],w[N],v[N];
string res;
int n,m;
struct node{
	string way;
	int index;
	int sum;
};

void bfs()
{
	queue<node> q;
	node t;
	t.way="";
	t.sum=0;
	t.index=0;
	q.push(t);

	while(!q.empty())
	{
		node now=q.front();
		q.pop();
		//printf("index=%d sum=%d \n",now.index,now.sum);
		//cout<<now.way<<endl<<endl;
		if(now.sum==f[m])
		{
			int len=now.way.length();
			int t_w=0;
			for(int i=0;i<len;i++)
				t_w+=w[now.way[i]-'0'];
			if(t_w<=m)
			{
				res=now.way;
				break;
			}
		}
		for(int i=1+now.index;i<=n;i++)
		{
			node temp=now;
			temp.way+=(i+'0');
			temp.sum=now.sum+v[i];
			temp.index=i;
			q.push(temp);
		}
	}

	int len=res.length();
	for(int i=0;i<len;i++)
		printf("%c ",res[i]);
	return ;
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&w[i],&v[i]);

	for(int i=1;i<=n;i++)
	{
		for(int j=m;j>=w[i];j--)
		{
			f[j]=max(f[j],f[j-w[i]]+v[i]);
		}
	}

	printf("%lld\n",f[m]);
	bfs();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
typedef long long ll;
int f[N],w[N],v[N];
string res;
int n,m;
struct node{
	int way[N];
	int cnt;
	int index;
	int sum_v;

};

void bfs()
{
	queue<node> q;
	node t;
	t.sum_v=0;
	t.cnt=0;
	t.index=0;
	q.push(t);

	while(!q.empty())
	{
		node now=q.front();
		q.pop();
		//printf("index=%d  v=%d   w=%d \n",now.index,now.sum_v,now.sum_w);
		if(now.sum_v==f[m])
		{
			int sum_w=0;
			for(int i=0;i<now.cnt;i++)
				sum_w+=w[now.way[i]];
			//printf("%d \n",sum_w);
			if(sum_w<=m)
			{
				for(int i=0;i<now.cnt;i++)
					printf("%d ",now.way[i]);
				return ;
			}
		}
		else
		{
			for(int i=1+now.index;i<=n;i++)
			{
				node temp=now;
				temp.way[now.cnt]=i;
				temp.cnt=now.cnt+1;
				temp.sum_v=now.sum_v+v[i];
				//temp.sum_w=now.sum_w+w[i];
				temp.index=i;
				q.push(temp);
			}
		}
	}

	return ;
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&w[i],&v[i]);

	for(int i=1;i<=n;i++)
	{
		for(int j=m;j>=w[i];j--)
		{
			f[j]=max(f[j],f[j-w[i]]+v[i]);
		}
	}

	printf("%d\n",f[m]);
	bfs();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
typedef long long ll;
int f[N],w[N],v[N];
string res;
int n,m;
struct node{
	string way;
	int index;
	int sum;
};

void bfs()
{
	queue<node> q;
	node t;
	t.way="";
	t.sum=0;
	t.index=0;
	q.push(t);

	while(!q.empty())
	{
		node now=q.front();
		q.pop();
		//printf("index=%d sum=%d \n",now.index,now.sum);
		//cout<<now.way<<endl<<endl;
		if(now.sum==f[m])
		{
			int len=now.way.length();
			int t_w=0;
			for(int i=0;i<len;i++)
				t_w+=w[now.way[i]-'0'];
			if(t_w<=m)
			{
				res=now.way;
				break;
			}
		}
		else
		{
			for(int i=1+now.index;i<=n;i++)
			{
				node temp=now;
				temp.way+=(i+'0');
				temp.sum=now.sum+v[i];
				temp.index=i;
				q.push(temp);
			}
		}
	}

	int len=res.length();
	for(int i=0;i<len;i++)
		printf("%c\n",res[i]);
	return ;
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&w[i],&v[i]);

	for(int i=1;i<=n;i++)
	{
		for(int j=m;j>=w[i];j--)
		{
			f[j]=max(f[j],f[j-w[i]]+v[i]);
		}
	}

	printf("%lld\n",f[m]);
	bfs();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
typedef long long ll;
int f[N],w[N],v[N];
string res;
int n,m;
struct node{
	int way[N];
	int cnt;
	int index;
	int sum_v;

};

void bfs()
{
	queue<node> q;
	node t;
	t.sum_v=0;
	t.cnt=0;
	t.index=0;
	q.push(t);

	while(!q.empty())
	{
		node now=q.front();
		q.pop();
		//printf("index=%d  v=%d   w=%d \n",now.index,now.sum_v,now.sum_w);
		if(now.sum_v==f[m])
		{
			int sum_w=0;
			for(int i=0;i<now.cnt;i++)
				sum_w+=w[now.way[i]];
			if(sum_w<=m)
			{
				for(int i=0;i<now.cnt;i++)
					printf("%d ",now.way[i]);
			}
			return ;
		}
		else
		{
			for(int i=1+now.index;i<=n;i++)
			{
				node temp=now;
				temp.way[now.cnt]=i;
				temp.cnt=now.cnt+1;
				temp.sum_v=now.sum_v+v[i];
				//temp.sum_w=now.sum_w+w[i];
				temp.index=i;
				q.push(temp);
			}
		}
	}

	return ;
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&w[i],&v[i]);

	for(int i=1;i<=n;i++)
	{
		for(int j=m;j>=w[i];j--)
		{
			f[j]=max(f[j],f[j-w[i]]+v[i]);
		}
	}

	printf("%d\n",f[m]);
	bfs();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
typedef pair<int,int> pill;
priority_queue<pill,vector<pill>,greater<pill> > q;
struct node{
	int pos;
	int w;
	int v;
}t[N];

int cmp(node s1,node s2)
{
	if(s1.w==s2.w)
		return s1.v>s2.v;
	else
		return s1.w<s2.w;
}

int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&t[i].w,&t[i].v),t[i].pos=i;
	sort(t+1,t+1+n,cmp);

	int now_w=0,now_v=0,i;
	for(i=1;i<=n;i++)
	{
		if(now_w+t[i].w>m)
			break;
	//	printf("%d ",t[i].v);
		q.push(make_pair(t[i].v,i));
		now_w+=t[i].w;
		now_v+=t[i].v;
	}
//	printf("\n%d %d \n\n",now_w,now_v);
	for(;i<=n;i++)
	{
		//printf("%d %d %d\n",t[i].v,t[i].w,q.top().first);
		if(t[i].v>=q.top().first)
		{
			int k=q.top().second;

			if(now_w-t[k].w+t[i].w<=m)
			{
				now_w=now_w-t[k].w+t[i].w;
				now_v=now_v-t[k].v+t[i].v;
				q.pop();
				q.push(make_pair(t[i].v,i));
			}
			else
			{
				queue<pill> tq;
				int tw=now_w+t[i].w;
				int tv=0;
				while(tw>m)
				{
					k=q.top().second;
					tv+=q.top().first;
					tq.push(make_pair(q.top().first,k));
					tw-=t[k].w;
				//	printf("v=%d w=%d tw=%d \n",q.top().first,t[k].w,tw);
					q.pop();
				}
				if(tv<t[i].v)
				{
					now_w=tw;
					now_v=now_v-tv+t[i].v;
					q.push(make_pair(t[i].v,i));
				}
				else
				{
					while(!tq.empty())
					{
						int a=tq.front().first;
						int b=tq.front().second;
						q.push(make_pair(a,b));
						tq.pop();
					}
				}
			}
		}
	//	printf("now_v=%d now_w=%d \n",now_v,now_w);
	}

	printf("%d\n",now_v);
	while(!q.empty())
	{
		int k=q.top().second;
		q.pop();
		printf("%d ",t[k].pos);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
typedef pair<int,int> pill;
priority_queue<pill,vector<pill>,greater<pill> > q;
struct node{
	int pos;
	int w;
	int v;
}t[N];

int cmp(node s1,node s2)
{
	if(s1.w==s2.w)
		return s1.v>s2.v;
	else
		return s1.w<s2.w;
}

int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&t[i].w,&t[i].v),t[i].pos=i;
	sort(t+1,t+1+n,cmp);

	int now_w=0,now_v=0,i=1;
	for(i=1;i<=n;i++)
	{
		if(now_w+t[i].w>m)
			break;
	//	printf("%d ",t[i].v);
		q.push(make_pair(t[i].v,i));
		now_w+=t[i].w;
		now_v+=t[i].v;
	}
//	printf("\n%d %d \n\n",now_w,now_v);
	if(q.size()==0)
	{
		printf("0\n");
		return 0;
	}

	for(;i<=n;i++)
	{
	//	printf("%d %d %d\n",t[i].v,t[i].w,q.top().first);
		if(t[i].v>=q.top().first)
		{
			int k=q.top().second;

			if(now_w-t[k].w+t[i].w<=m)
			{
				now_w=now_w-t[k].w+t[i].w;
				now_v=now_v-t[k].v+t[i].v;
				q.pop();
				q.push(make_pair(t[i].v,i));
			}
			else
			{
				queue<pill> tq;
				int tw=now_w+t[i].w;
				int tv=0;
				while(tw>m)
				{
					k=q.top().second;
					tv+=q.top().first;
					tq.push(make_pair(q.top().first,k));
					tw-=t[k].w;
				//	printf("v=%d w=%d tw=%d \n",q.top().first,t[k].w,tw);
					q.pop();
				}
				if(tv<t[i].v)
				{
					now_w=tw;
					now_v=now_v-tv+t[i].v;
					q.push(make_pair(t[i].v,i));
				}
				else
				{
					while(!tq.empty())
					{
						int a=tq.front().first;
						int b=tq.front().second;
						q.push(make_pair(a,b));
						tq.pop();
					}
				}
			}
		}
	//	printf("now_v=%d now_w=%d \n",now_v,now_w);
	}

	printf("%d\n",now_v);
	while(!q.empty())
	{
		int k=q.top().second;
		q.pop();
		printf("%d ",t[k].pos);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
typedef pair<int,int> pill;
priority_queue<pill,vector<pill>,greater<pill> > q;
struct node{
	int pos;
	int w;
	int v;
}t[N];

int cmp(node s1,node s2)
{
	if(s1.w==s2.w)
		return s1.v>s2.v;
	else
		return s1.w<s2.w;
}

int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&t[i].w,&t[i].v),t[i].pos=i;
	sort(t+1,t+1+n,cmp);

	int now_w=0,now_v=0,i=1;
	for(i=1;i<=n;i++)
	{
		if(now_w+t[i].w>m)
			break;
	//	printf("%d ",t[i].v);
		q.push(make_pair(t[i].v,i));
		now_w+=t[i].w;
		now_v+=t[i].v;
	}
//	printf("\n%d %d \n\n",now_w,now_v);
	if(q.size()==0)
	{
		printf("0\n");
		return 0;
	}

	for(;i<=n;i++)
	{
		printf("%d %d %d\n",t[i].v,t[i].w,q.top().first);
		if(t[i].v>=q.top().first)
		{
			int k=q.top().second;

			if(now_w-t[k].w+t[i].w<=m)
			{
				now_w=now_w-t[k].w+t[i].w;
				now_v=now_v-t[k].v+t[i].v;
				q.pop();
				q.push(make_pair(t[i].v,i));
			}
			else
			{
				queue<pill> tq;
				int tw=now_w+t[i].w;
				int tv=0;
				while(tw>m)
				{
					k=q.top().second;
					tv+=q.top().first;
					tq.push(make_pair(q.top().first,k));
					tw-=t[k].w;
				//	printf("v=%d w=%d tw=%d \n",q.top().first,t[k].w,tw);
					q.pop();
				}
				if(tv<t[i].v)
				{
					now_w=tw;
					now_v=now_v-tv+t[i].v;
					q.push(make_pair(t[i].v,i));
				}
				else
				{
					while(!tq.empty())
					{
						int a=tq.front().first;
						int b=tq.front().second;
						q.push(make_pair(a,b));
						tq.pop();
					}
				}
			}
		}
	//	printf("now_v=%d now_w=%d \n",now_v,now_w);
	}

	printf("%d\n",now_v);
	while(!q.empty())
	{
		int k=q.top().second;
		q.pop();
		printf("%d ",t[k].pos);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
typedef long long ll;
int f[N],w[N],v[N];
string res;
int n,m;
struct node{
	string way;
	int index;
	int sum;
};

void bfs()
{
	queue<node> q;
	node t;
	t.way="";
	t.sum=0;
	t.index=0;
	q.push(t);

	while(!q.empty())
	{
		node now=q.front();
		q.pop();
		//printf("index=%d sum=%d \n",now.index,now.sum);
		//cout<<now.way<<endl<<endl;
		if(now.sum==f[m])
		{
			int len=now.way.length();
			int t_w=0;
			for(int i=0;i<len;i++)
				t_w+=w[now.way[i]-'0'];
			if(t_w<=m)
			{
				res=now.way;
				break;
			}
		}
		else
		{
			for(int i=1+now.index;i<=n;i++)
			{
				node temp=now;
				temp.way+=(i+'0');
				temp.sum=now.sum+v[i];
				temp.index=i;
				q.push(temp);
			}
		}
	}

	int len=res.length();
	for(int i=0;i<len;i++)
		printf("%c\n",res[i]);
	return ;
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&w[i],&v[i]);

	for(int i=1;i<=n;i++)
	{
		for(int j=m;j>=w[i];j--)
		{
			f[j]=max(f[j],f[j-w[i]]+v[i]);
		}
	}

	printf("%d\n",f[m]);
	bfs();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
typedef long long ll;
int f[N],w[N],v[N];
string res;
int n,m;
struct node{
	int way[N];
	int cnt;
	int index;
	int sum_v;
	int sum_w;
};

void bfs()
{
	queue<node> q;
	node t;
	t.sum_v=0;
	t.sum_w=0;
	t.cnt=0;
	t.index=0;
	q.push(t);

	while(!q.empty())
	{
		node now=q.front();
		q.pop();
		//printf("index=%d  v=%d   w=%d \n",now.index,now.sum_v,now.sum_w);
		if(now.sum_v==f[m]&&now.sum_w<=m)
		{
			for(int i=0;i<now.cnt;i++)
				printf("%d ",now.way[i]);
			return ;
		}
		else
		{
			for(int i=1+now.index;i<=n;i++)
			{
				node temp=now;
				temp.way[now.cnt]=i;
				temp.cnt=now.cnt+1;
				temp.sum_v=now.sum_v+v[i];
				temp.sum_w=now.sum_w+w[i];
				temp.index=i;
				q.push(temp);
			}
		}
	}

	return ;
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&w[i],&v[i]);

	for(int i=1;i<=n;i++)
	{
		for(int j=m;j>=w[i];j--)
		{
			f[j]=max(f[j],f[j-w[i]]+v[i]);
		}
	}

	printf("%d\n",f[m]);
	bfs();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
int f[N],a[N],b[N];
int vis[N];
int n,m;
struct node{
	int a;
	int b;
	int pos;
}table[N];

int cmp(node s1,node s2)
{
	if(s1.b==s2.b )
		return s1.a>s2.a;
	else
		return s1.b<s2.b;
}

int main()
{
	int v=0,w=0;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&table[i].a,&table[i].b),v+=table[i].a,w+=table[i].b ,table[i].pos=i;

	if(v<=m)
	{
		printf("%d \n",w);
		for(int i=1;i<=n;i++)
			printf("%d ",i);
	}
	else
	{
		sort(table+1,table+n+1,cmp);
		for(int i=1;i<=n;i++)
		{
			vis[table[i].pos]=1;
			v-=table[i].a;
			w-=table[i].b;
			if(v<=m)
			{
				printf("%d\n",w);
				for(int i=1;i<=n;i++)
					if(!vis[i])
						printf("%d ",i);
				break;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
typedef long long ll;
int f[N],w[N],v[N];
string res;
int n,m;
struct node{
	string way;
	int index;
	int sum;
};

void bfs()
{
	queue<node> q;
	node t;
	t.way="";
	t.sum=0;
	t.index=0;
	q.push(t);

	while(!q.empty())
	{
		node now=q.front();
		q.pop();
		//printf("index=%d sum=%d \n",now.index,now.sum);
		//cout<<now.way<<endl<<endl;
		if(now.sum==f[m])
		{
			int len=now.way.length();
			int t_w=0;
			for(int i=0;i<len;i++)
				t_w+=w[now.way[i]-'0'];
			if(t_w<=m)
			{
				res=now.way;
				break;
			}
		}
		else
		{
			for(int i=1+now.index;i<=n;i++)
			{
				node temp=now;
				temp.way+=(i+'0');
				temp.sum=now.sum+v[i];
				temp.index=i;
				q.push(temp);
			}
		}
	}

	int len=res.length();
	for(int i=0;i<len;i++)
		printf("%c ",res[i]);
	return ;
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&w[i],&v[i]);

	for(int i=1;i<=n;i++)
	{
		for(int j=m;j>=w[i];j--)
		{
			f[j]=max(f[j],f[j-w[i]]+v[i]);
		}
	}

	printf("%lld\n",f[m]);
	bfs();
	return 0;
}


#include<iostream>
#include<algorithm>
#include <functional>
using namespace std;
int k[10002],c[10002],ki,ci,kn,cn,n,V,Tn,a[10002],an,vn=0;
void input(int n);
void work(int v){
    if((ki+1 < kn && ci < cn && k[ki]+k[ki+1] > c[ci])||(ki < kn && ci < cn && k[ki] >= c[ci])||(ki < kn && ci >= cn)){
      if(v>0){
        a[an++]=1;
        vn+=k[ki];
          ki++;
        work(v-1);
      }
    }
    else if(ki < kn && ci < cn && v >= 2){
      a[an++]=2;
      vn+=c[ci];
      ci++;
      work(v-2);
    }
}
int main(){
  cin>>n>>V;
  input(n);
  sort(k,k+kn,greater<int>());
  sort(c,c+cn,greater<int>());

  work(V);
  cout<<vn<<endl;
  for(int i = 0 ; i < an ; i++){
    cout<<a[i];
  }

  return 0;
}
void input(int n){
    int l,m;
    cn=0;
    kn=0;
  for(int i = 0 ; i < n  ; i++){
    cin>>l>>m;
    if(l==1){
      k[kn++]=m;
    }
    else{
      c[cn++]=m;
    }
  }
}


#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int n , w ;
struct vehicle{
    int p ;
    int ind ;
    int v;
};

bool cp(vehicle& a , vehicle& b){
    return a.p > b.p;
}

int main(){
    cin >> n >> w ;
    vector<vehicle> kayaks , catamarans;
    for(int i = 0 ; i < n ; i++){
        vehicle v1;
        cin >> v1.v >> v1.p ;
        v1.ind = (i+1);
        if(v1.v == 1){
            kayaks.push_back(v1);
        }else{
            catamarans.push_back(v1);
        }
    }

    sort(kayaks.begin(),kayaks.end(),cp);
    sort(catamarans.begin(),catamarans.end(),cp);

    int nn = kayaks.size() , mm = catamarans.size();

    if(nn > w)nn = w ;
    if(2 * mm > w)mm = w/2 + 1;

    vector<int> ksum(nn+1,0) , csum(mm+1,0);

    for(int i = 1 ; i < nn+1 ; i++){
        ksum[i] = ksum[i-1] + kayaks[i-1].p;
    }
    for(int i = 1 ; i < mm+1 ; i++){
        csum[i] = csum[i-1] + catamarans[i-1].p;
    }

    int max = -1 , ii = -1 , jj = -1 ;
    for(int i = 0 ; i < nn +1 ; i++){
        // for(int j = 0  ; j < mm + 1 ; j++){
            int j = (w-i)/2;
            if(j > mm) j = mm;
            // cout << csum[j] + ksum[i]  << "(" << 2*j+i << ")" << "   ";

            if(2*j+i <= w ){
                int temp = csum[j] + ksum[i];
                if(temp > max){
                    max = temp;
                    ii = i ;
                    jj = j ;
                }
            }
        // }
        // cout << endl;
    }

    cout << max << endl;
    for(int i = 0 ; i < ii ; i++){
        cout << kayaks[i].ind << " ";
    }
    for(int j = 0 ; j < jj ; j++){
        cout << catamarans[j].ind << " ";
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int n , w ;
struct vehicle{
    int p ;
    int ind ;
    int v;
};

bool cp(vehicle& a , vehicle& b){
    return a.p > b.p;
}

int main(){
    cin >> n >> w ;
    vector<vehicle> kayaks , catamarans;
    for(int i = 0 ; i < n ; i++){
        vehicle v1;
        cin >> v1.v >> v1.p ;
        v1.ind = (i+1);
        if(v1.v == 1){
            kayaks.push_back(v1);
        }else{
            catamarans.push_back(v1);
        }
    }

    sort(kayaks.begin(),kayaks.end(),cp);
    sort(catamarans.begin(),catamarans.end(),cp);

    int nn = kayaks.size() , mm = catamarans.size();

    if(nn > w)nn = w ;
    if(2 * mm > w)mm = w/2 + 1;

    vector<int> ksum(nn+1,0) , csum(mm+1,0);

    for(int i = 1 ; i < nn+1 ; i++){
        ksum[i] = ksum[i-1] + kayaks[i-1].p;
    }
    for(int i = 1 ; i < mm+1 ; i++){
        csum[i] = csum[i-1] + catamarans[i-1].p;
    }

    int max = -1 , ii = -1 , jj = -1 ;
    for(int i = nn/2 ; i < nn +1 ; i++){
        for(int j = mm/2  ; j < mm + 1 ; j++){
            // cout << vec[i][j]  << "(" << 2*j+i << ")" << "   ";
            if(2*j+i <= w ){
                int temp = csum[j] + ksum[i];
                if(temp > max){
                    max = temp;
                    ii = i ;
                    jj = j ;
                }
            }
        }
        // cout << endl;
    }

    cout << max << endl;
    for(int i = 0 ; i < ii ; i++){
        cout << kayaks[i].ind << " ";
    }
    for(int j = 0 ; j < jj ; j++){
        cout << catamarans[j].ind << " ";
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int n , w ;
struct vehicle{
    int p ;
    int ind ;
    int v;
};

bool cp(vehicle& a , vehicle& b){
    return a.p > b.p;
}

int main(){
    cin >> n >> w ;
    vector<vehicle> kayaks , catamarans;
    for(int i = 0 ; i < n ; i++){
        vehicle v1;
        cin >> v1.v >> v1.p ;
        v1.ind = (i+1);
        if(v1.v == 1){
            kayaks.push_back(v1);
        }else{
            catamarans.push_back(v1);
        }
    }

    sort(kayaks.begin(),kayaks.end(),cp);
    sort(catamarans.begin(),catamarans.end(),cp);

    int nn = kayaks.size() , mm = catamarans.size();

    if(nn > w)nn = w ;
    if(2 * mm > w)mm = w/2 + 1;

    int vec[nn+1][mm+1];
    vec[0][0] = 0;
    for(int i = 1 ; i < nn+1; i++){
        vec[i][0] = vec[i-1][0] + kayaks[i-1].p;
    }
    for(int j = 1 ; j < mm +1 ; j++){
        vec[0][j] = vec[0][j-1] + catamarans[j-1].p;
    }
    for(int i = 1 ; i < nn+1 ; i++){
        for(int j = 1 ; j < mm+1 ; j++){
            int x = vec[i-1][j] + kayaks[i-1].p;
            int y = vec[i][j-1] + catamarans[j-1].p;
            vec[i][j] = (x > y) ? x : y ;
        }
    }

    int max = -1 , ii = -1 , jj = -1 ;
    for(int i = 0 ; i < nn +1 ; i++){
        for(int j = 0  ; j < mm + 1 ; j++){
            // cout << vec[i][j]  << "(" << 2*j+i << ")" << "   ";
            if(2*j+i <= w && max < vec[i][j]){
                max = vec[i][j];
                ii = i ;
                jj = j ;
            }
        }
        // cout << endl;
    }

    cout << max << endl;
    for(int i = 0 ; i < ii ; i++){
        cout << kayaks[i].ind << " ";
    }
    for(int j = 0 ; j < jj ; j++){
        cout << catamarans[j].ind << " ";
    }
    return 0;
}


#include<iostream>
using namespace std;
struct node{
  int key , value ; // key stores position data stores value
} k[10002] , c[10002];
int ki,ci,kn,cn,n,V,Tn,a[10002],an,vn=0;
void input(int n);
void work(int v){
    if((ki+1 < kn && ci < cn && k[ki].value+k[ki+1].value > c[ci].value)||(ki < kn && ci < cn && k[ki].value >= c[ci].value)||(ki < kn && ci >= cn)){
      if(v>0){
        a[an++]=k[ki].key;
        vn+=k[ki].value;
          ki++;
        work(v-1);
      }
    }
    else if(ki < kn && ci < cn && v >= 2){
      a[an++]=c[ci].key;
      vn+=c[ci].value;
      ci++;
      work(v-2);
    }
}
void sortt( node * a , int n ){
  for(int j=0;j<n;j++)
	{
	int flag=0;
		for(int i=0;i<n-1-j;i++)
		{
				if((a+i)->value >(a+i+1)->value)
				{
					int t1 = (a+i)->value , t2 = (a+i)->key;
          (a+i)->value = (a+i+1)->value ;
          (a+i)->key = (a+i+1)->key;
          (a+i+1)->value = t1;
          (a+i+1)->key = t2 ;
					flag=1;
				}

		}
		if (flag==0) break;
	}
}
int main(){
  cin>>n>>V;
  input(n);
  sortt(&k[0],kn);
  sortt(&c[0],cn);

  work(V);
  cout<<vn<<endl;
  for(int i = an-1 ; i >=0  ; i--){
    cout<<a[i]<<" ";
  }

  return 0;
}
void input(int n){
    int l,m;
    cn=0;
    kn=0;
  for(int i = 1 ; i <= n  ; i++){
    cin>>l>>m;
    if(l==1){
      k[kn++].value=m;
      k[kn-1].key=i;
    }
    else{
      c[cn++].value=m;
      c[cn-1].key=i;
    }
  }
}


#include<iostream>
using namespace std;
struct node{
  int key , value ; // key stores position data stores value
} k[10002] , c[10002];
int ki,ci,kn,cn,n,V,Tn,a[10002],an,vn=0;
void input(int n);
void work(int v){
    if((ki < kn && ci < cn && k[ki].value >= c[ci].value)||(ki < kn && ci >= cn)||(v<=1)){
      if(v>0){
        a[an++]=k[ki].key;
        vn+=k[ki].value;
          ki++;
        work(v-1);
      }
    }
    else if((ki+1 < kn && ci < cn && (k[ki].value+k[ki+1].value > c[ci].value))&&v >= 2){
      a[an++]=k[ki].key;
      vn+=k[ki].value;
        ki++;
        a[an++]=k[ki].key;
        vn+=k[ki].value;
          ki++;
      work(v-2);
    }
    else if(ki < kn && ci < cn && v >= 2){
      a[an++]=c[ci].key;
      vn+=c[ci].value;
      ci++;
      work(v-2);
    }
}
void sortt( node * a , int n ){
  for(int j=0;j<n;j++)
	{
	int flag=0;
		for(int i=0;i<n-1-j;i++)
		{
				if((a+i)->value < (a+i+1)->value)
				{
					int t1 = (a+i)->value , t2 = (a+i)->key;
          (a+i)->value = (a+i+1)->value ;
          (a+i)->key = (a+i+1)->key;
          (a+i+1)->value = t1;
          (a+i+1)->key = t2 ;
					flag=1;
				}

		}
		if (flag==0) break;
	}
}
int main(){
  cin>>n>>V;
  input(n);
  sortt(&k[0],kn);
  sortt(&c[0],cn);
  // for(int i = 0; i < kn ; i++){
  //   cout<<k[i].value<<":"<<k[i].key<<"     ";
  // }
  // cout<<endl;
  // for(int i = 0; i < cn ; i++){
  //   cout<<c[i].value<<":"<<c[i].key<<"     ";
  // }
  work(V);
  cout<<vn<<endl;
  for(int i = 0 ; i <an  ; i++){
    cout<<a[i]<<" ";
  }

  return 0;
}
void input(int n){
    int l,m;
    cn=0;
    kn=0;
  for(int i = 1 ; i <= n  ; i++){
    cin>>l>>m;
    if(l==1){
      k[kn++].value=m;
      k[kn-1].key=i;
    }
    else{
      c[cn++].value=m;
      c[cn-1].key=i;
    }
  }
}


#include<iostream>
using namespace std;
struct node{
  int key , value ; // key stores position data stores value
} k[100002] , c[100002];
int ki,ci,kn,cn,n,V,Tn,a[100002],an,vn=0;
void input(int n);
void work(int v){
    if(ki < kn && ci < cn){
      if(v==1){
        a[an]=k[ki].key;
        vn+=k[ki].value;
        ki++;
        an++;
        work(v-1);
      }
      else if(v > 0){
        if(k[ki].value > c[ci].value){
          a[an]=k[ki].key;
          vn+=k[ki].value;
          ki++;
          an++;
          work(v-1);
        }
        else{
          if(ki+1 < kn && k[ki].value + k[ki+1].value > c[ci].value ){
            a[an]=k[ki].key;
            vn+=k[ki].value;
            ki++;
            an++;
            a[an]=k[ki].key;
            vn+=k[ki].value;
            ki++;
            an++;
            work(v-2);
          }
          else{
            a[an]=c[ci].key;
            vn+=c[ci].value;
            ci++;
            an++;
            work(v-2);
          }
        }
      }
    }
    else if(ki < kn && v >= 1){
      a[an]=k[ki].key;
      vn+=k[ki].value;
      ki++;
      an++;
      work(v-1);
    }
    else if(ci < cn && v >= 2){
      a[an]=c[ci].key;
      vn+=c[ci].value;
      ci++;
      an++;
      work(v-2);
    }
}
void sortt( node * a , int n ){
  for(int j=0;j<n;j++)
	{
	int flag=0;
		for(int i=0;i<n-1-j;i++)
		{
				if((a+i)->value < (a+i+1)->value)
				{
					int t1 = (a+i)->value , t2 = (a+i)->key;
          (a+i)->value = (a+i+1)->value ;
          (a+i)->key = (a+i+1)->key;
          (a+i+1)->value = t1;
          (a+i+1)->key = t2 ;
					flag=1;
				}

		}
		if (flag==0) break;
	}
}
int main(){
  cin>>n>>V;
  input(n);
  sortt(&k[0],kn);
  sortt(&c[0],cn);
  // for(int i = 0; i < kn ; i++){
  //   cout<<k[i].value<<":"<<k[i].key<<"     ";
  // }
  // cout<<endl;
  // for(int i = 0; i < cn ; i++){
  //   cout<<c[i].value<<":"<<c[i].key<<"     ";
  // }
  work(V);
  cout<<vn<<endl;
  for(int i = 0 ; i <an  ; i++){
    cout<<a[i]<<" ";
  }

  return 0;
}
void input(int n){
    int l,m;
    cn=0;
    kn=0;
  for(int i = 1 ; i <= n  ; i++){
    cin>>l>>m;
    if(l==1){
      k[kn++].value=m;
      k[kn-1].key=i;
    }
    else{
      c[cn++].value=m;
      c[cn-1].key=i;
    }
  }
}


#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int n , w ;
struct vehicle{
    int p ;
    int ind ;
    int v;
};

bool cp(vehicle& a , vehicle& b){
    return a.p > b.p;
}

int main(){
    cin >> n >> w ;
    vector<vehicle> kayaks , catamarans;
    for(int i = 0 ; i < n ; i++){
        vehicle v1;
        cin >> v1.v >> v1.p ;
        v1.ind = (i+1);
        if(v1.v == 1){
            kayaks.push_back(v1);
        }else{
            catamarans.push_back(v1);
        }
    }

    sort(kayaks.begin(),kayaks.end(),cp);
    sort(catamarans.begin(),catamarans.end(),cp);

    int nn = kayaks.size() , mm = catamarans.size();

    int vec[nn+1][mm+1];
    vec[0][0] = 0;
    for(int i = 1 ; i < nn+1; i++){
        vec[i][0] = vec[i-1][0] + kayaks[i-1].p;
    }
    for(int j = 1 ; j < mm +1 ; j++){
        vec[0][j] = vec[0][j-1] + catamarans[j-1].p;
    }
    for(int i = 1 ; i < nn+1 ; i++){
        for(int j = 1 ; j < mm+1 ; j++){
            int x = vec[i-1][j] + kayaks[i-1].p;
            int y = vec[i][j-1] + catamarans[j-1].p;
            vec[i][j] = (x > y) ? x : y ;
        }
    }

    int max = -1 , ii = -1 , jj = -1 ;
    for(int i = 0 ; i < nn +1 ; i++){
        for(int j = 0  ; j < mm + 1 ; j++){
            // cout << vec[i][j]  << "(" << 2*j+i << ")" << "   ";
            if(2*j+i <= w && max < vec[i][j]){
                max = vec[i][j];
                ii = i ;
                jj = j ;
            }
        }
        // cout << endl;
    }

    cout << max << endl;
    for(int i = 0 ; i < ii ; i++){
        cout << kayaks[i].ind << " ";
    }
    for(int j = 0 ; j < jj ; j++){
        cout << catamarans[j].ind << " ";
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int n , w ;
struct vehicle{
    int p ;
    int ind ;
    int v;
};

bool cp(vehicle& a , vehicle& b){
    return a.p > b.p;
}

int main(){
    cin >> n >> w ;
    vector<vehicle> kayaks , catamarans;
    for(int i = 0 ; i < n ; i++){
        vehicle v1;
        cin >> v1.v >> v1.p ;
        v1.ind = (i+1);
        if(v1.v == 1){
            kayaks.push_back(v1);
        }else{
            catamarans.push_back(v1);
        }
    }

    sort(kayaks.begin(),kayaks.end(),cp);
    sort(catamarans.begin(),catamarans.end(),cp);

    int nn = kayaks.size() , mm = catamarans.size();

    if(nn > w)nn = w ;
    if(2 * mm > w)mm = w/2 + 1;

    vector<int> ksum(nn+1,0) , csum(mm+1,0);

    for(int i = 1 ; i < nn+1 ; i++){
        ksum[i] = ksum[i-1] + kayaks[i-1].p;
    }
    for(int i = 1 ; i < mm+1 ; i++){
        csum[i] = csum[i-1] + catamarans[i-1].p;
    }

    int max = -1 , ii = -1 , jj = -1 ;
    for(int i = 0 ; i < nn +1 ; i++){
        for(int j = 0  ; j < mm + 1 ; j++){
            // cout << vec[i][j]  << "(" << 2*j+i << ")" << "   ";
            if(2*j+i <= w ){
                int temp = csum[j] + ksum[i];
                if(temp > max){
                    max = temp;
                    ii = i ;
                    jj = j ;
                }
            }
        }
        // cout << endl;
    }

    cout << max << endl;
    for(int i = 0 ; i < ii ; i++){
        cout << kayaks[i].ind << " ";
    }
    for(int j = 0 ; j < jj ; j++){
        cout << catamarans[j].ind << " ";
    }
    return 0;
}


#include<iostream>
using namespace std;
struct node{
  int key , value ; // key stores position data stores value
} k[10002] , c[10002];
int ki,ci,kn,cn,n,V,Tn,a[10002],an,vn=0;
void input(int n);
void work(int v){
    if(ki < kn && ci < cn){
      if(v==1){
        a[an]=k[ki].key;
        vn+=k[ki].value;
        ki++;
        an++;
        work(v-1);
      }
      else if(v > 0){
        if(k[ki].value > c[ci].value){
          a[an]=k[ki].key;
          vn+=k[ki].value;
          ki++;
          an++;
          work(v-1);
        }
        else{
          if(ki+1 < kn && k[ki].value + k[ki+1].value > c[ci].value ){
            a[an]=k[ki].key;
            vn+=k[ki].value;
            ki++;
            an++;
            a[an]=k[ki].key;
            vn+=k[ki].value;
            ki++;
            an++;
            work(v-2);
          }
          else{
            a[an]=c[ci].key;
            vn+=c[ci].value;
            ci++;
            an++;
            work(v-2);
          }
        }
      }
    }
    else if(ki < kn && v >= 1){
      a[an]=k[ki].key;
      vn+=k[ki].value;
      ki++;
      an++;
      work(v-1);
    }
    else if(ci < cn && v >= 2){
      a[an]=c[ci].key;
      vn+=c[ci].value;
      ci++;
      an++;
      work(v-2);
    }
}
void sortt( node * a , int n ){
  for(int j=0;j<n;j++)
	{
	int flag=0;
		for(int i=0;i<n-1-j;i++)
		{
				if((a+i)->value < (a+i+1)->value)
				{
					int t1 = (a+i)->value , t2 = (a+i)->key;
          (a+i)->value = (a+i+1)->value ;
          (a+i)->key = (a+i+1)->key;
          (a+i+1)->value = t1;
          (a+i+1)->key = t2 ;
					flag=1;
				}

		}
		if (flag==0) break;
	}
}
int main(){
  cin>>n>>V;
  input(n);
  sortt(&k[0],kn);
  sortt(&c[0],cn);
  // for(int i = 0; i < kn ; i++){
  //   cout<<k[i].value<<":"<<k[i].key<<"     ";
  // }
  // cout<<endl;
  // for(int i = 0; i < cn ; i++){
  //   cout<<c[i].value<<":"<<c[i].key<<"     ";
  // }
  work(V);
  cout<<vn<<endl;
  for(int i = 0 ; i <an  ; i++){
    cout<<a[i]<<" ";
  }

  return 0;
}
void input(int n){
    int l,m;
    cn=0;
    kn=0;
  for(int i = 1 ; i <= n  ; i++){
    cin>>l>>m;
    if(l==1){
      k[kn++].value=m;
      k[kn-1].key=i;
    }
    else{
      c[cn++].value=m;
      c[cn-1].key=i;
    }
  }
}


#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int n , w ;
struct vehicle{
    int p ;
    int ind ;
    int v;
};

bool cp(vehicle& a , vehicle& b){
    return a.p > b.p;
}

int main(){
    cin >> n >> w ;
    vector<vehicle> kayaks , catamarans;
    for(int i = 0 ; i < n ; i++){
        vehicle v1;
        cin >> v1.v >> v1.p ;
        v1.ind = (i+1);
        if(v1.v == 1){
            kayaks.push_back(v1);
        }else{
            catamarans.push_back(v1);
        }
    }

    sort(kayaks.begin(),kayaks.end(),cp);
    sort(catamarans.begin(),catamarans.end(),cp);

    int nn = kayaks.size() , mm = catamarans.size();

    if(nn > w)nn = w ;
    if(2 * mm > w)mm = w/2 + 1;

    vector<int> ksum(nn+1,0) , csum(mm+1,0);

    for(int i = 1 ; i < nn+1 ; i++){
        ksum[i] = ksum[i-1] + kayaks[i-1].p;
    }
    for(int i = 1 ; i < mm+1 ; i++){
        csum[i] = csum[i-1] + catamarans[i-1].p;
    }

    int max = -1 , ii = -1 , jj = -1 ;
    for(int i = 0 ; i < nn +1 ; i++){
        // for(int j = 0  ; j < mm + 1 ; j++){
            int j = (w-i)/2;
            // cout << csum[j] + ksum[i]  << "(" << 2*j+i << ")" << "   ";

            if(2*j+i <= w ){
                int temp = csum[j] + ksum[i];
                if(temp > max){
                    max = temp;
                    ii = i ;
                    jj = j ;
                }
            }
        // }
        // cout << endl;
    }

    cout << max << endl;
    for(int i = 0 ; i < ii ; i++){
        cout << kayaks[i].ind << " ";
    }
    for(int j = 0 ; j < jj ; j++){
        cout << catamarans[j].ind << " ";
    }
    return 0;
}


#include <bits/stdc++.h>
#define NN 100005

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
PII Lo[NN];
LL od[NN];

int cmp(int a, int b){
	PII l1 = Lo[a], l2 = Lo[b];
	if(l1.first == 1) l1.second *= 2;
	if(l2.first == 1) l2.second *= 2;
	if(l1.second == l2.second){
		return (Lo[a].first < Lo[b].first);
	}
	return (l1.second > l2.second);
}

int main(){
	LL n, v;
	scanf("%lld%lld", &n, &v);
	for(int i = 0; i<n; i++){
		scanf("%lld%lld", &Lo[i].first, &Lo[i].second);
		od[i] = i;
	}
	sort(od, od + n, cmp);
	LL ans = 0, cur = 0, cnt = 0;
	while(cur < v){
		cur += Lo[od[cnt]].first;
		if(cnt < n - 1 && cur > v){
			cur -= Lo[od[cnt]].first;
			cnt ++;
			continue;
		}
		ans += Lo[od[cnt]].second;
		cnt ++;
	}
	printf("%lld\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#define NN 100005

using namespace std;

typedef long long LL;
typedef pair<LL, LL> PII;
PII Lo[NN];
LL od[NN];

int cmp(int a, int b){
	PII l1 = Lo[a], l2 = Lo[b];
	if(l1.first == 1) l1.second *= 2;
	if(l2.first == 1) l2.second *= 2;
	if(l1.second == l2.second){
		return (Lo[a].first < Lo[b].first);
	}
	return (l1.second > l2.second);
}

int main(){
	LL n, v;
	scanf("%lld%lld", &n, &v);
	for(int i = 0; i<n; i++){
		scanf("%lld%lld", &Lo[i].first, &Lo[i].second);
		od[i] = i;
	}
	sort(od, od + n, cmp);
	LL ans = 0, cur = 0, cnt = 0;
	while(cur < v){
		cur += Lo[od[cnt]].first;
		if(cnt < n - 1 && cur > v){
			cur -= Lo[od[cnt]].first;
			cnt ++;
			continue;
		}
		ans += Lo[od[cnt]].second;
		cnt ++;
	}
	printf("%lld\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

vector<pair<int, int> > v[3];
vector<int> ret;

int main() {
	//freopen("in.txt", "r", stdin);

	int n, vol; scanf("%d%d", &n, &vol);
	for (int tc = 1; tc <= n; tc++) {
		int t, p; scanf("%d%d", &t, &p);
		v[t].push_back(make_pair(p, tc));
	}
	for (int t = 1; t <= 2; t++) {
		sort(v[t].begin(), v[t].end());
		reverse(v[t].begin(), v[t].end());
		for (int i = 1; i < v[t].size(); i++) v[t][i].first += v[t][i - 1].first;
	}
	int ans = 0, p[3] = {-1, -1, -1};
	for (int i = -1; i < (int)v[2].size(); i++) {
		int rem = vol - 2 * (i + 1);
		if (rem < 0) break;
		rem = min(rem, (int)v[1].size());
		int cur = (i == -1 ? 0 : v[2][i].first) + (rem == 0 ? 0 : v[1][rem - 1].first);
		if (ans < cur) ans = cur, p[2] = i, p[1] = rem - 1;
	}
	printf("%d\n", ans);
	for (int t = 1; t <= 2; t++)
		for (int i = 0; i <= p[t]; i++) ret.push_back(v[t][i].second);//printf("%d ", v[t][i].second);
	sort(ret.begin(), ret.end());
	for (int i = 0; i < ret.size(); i++) printf("%d ", ret[i]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

vector<pair<int, int> > v[3];

int main() {
	//freopen("in.txt", "r", stdin);

	int n, vol; scanf("%d%d", &n, &vol);
	for (int tc = 1; tc <= n; tc++) {
		int t, p; scanf("%d%d", &t, &p);
		v[t].push_back(make_pair(p, tc));
	}
	for (int t = 1; t <= 2; t++) {
		sort(v[t].begin(), v[t].end());
		reverse(v[t].begin(), v[t].end());
		for (int i = 1; i < v[t].size(); i++) v[t][i].first += v[t][i - 1].first;
	}
	int ans = 0, p[3] = {-1, -1, -1};
	for (int i = 0; i < v[2].size(); i++) {
		int rem = vol - 2 * (i + 1);
		if (rem < 0) break;
		rem = min(rem, (int)v[1].size());
		int cur = v[2][i].first + (rem == 0 ? 0 : v[1][rem - 1].first);
		if (ans < cur) ans = cur, p[2] = i, p[1] = rem - 1;
	}
	printf("%d\n", ans);
	for (int t = 1; t <= 2; t++)
		for (int i = 0; i <= p[t]; i++) printf("%d ", v[t][i].second);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

vector<pair<int, int> > v[3];
vector<int> ret;

int main() {
	//freopen("in.txt", "r", stdin);

	int n, vol; scanf("%d%d", &n, &vol);
	for (int tc = 1; tc <= n; tc++) {
		int t, p; scanf("%d%d", &t, &p);
		v[t].push_back(make_pair(p, tc));
	}
	for (int t = 1; t <= 2; t++) {
		sort(v[t].begin(), v[t].end());
		reverse(v[t].begin(), v[t].end());
		for (int i = 1; i < v[t].size(); i++) v[t][i].first += v[t][i - 1].first;
	}
	int ans = 0, p[3] = {-1, -1, -1};
	for (int i = -1; i < (int)v[2].size(); i++) {
		int rem = vol - 2 * (i + 1);
		if (rem < 0) break;
		rem = min(rem, (int)v[1].size());
		int cur = (i == -1 ? 0 : v[2][i].first) + (rem == 0 ? 0 : v[1][rem - 1].first);
		if (ans < cur) ans = cur, p[2] = i, p[1] = rem - 1;
	}
	printf("%d\n", ans);
	for (int t = 1; t <= 2; t++)
		for (int i = 0; i <= p[t]; i++) ret.push_back(v[t][i].second);//printf("%d ", v[t][i].second);
	//sort(ret.begin(), ret.end());
	for (int i = 0; i < ret.size(); i++) printf("%d ", ret[i]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

vector<pair<int, int> > v[3];
vector<int> ret;

int main() {
	//freopen("in.txt", "r", stdin);

	int n, vol; scanf("%d%d", &n, &vol);
	for (int tc = 1; tc <= n; tc++) {
		int t, p; scanf("%d%d", &t, &p);
		v[t].push_back(make_pair(p, tc));
	}
	for (int t = 1; t <= 2; t++) {
		sort(v[t].begin(), v[t].end());
		reverse(v[t].begin(), v[t].end());
		for (int i = 1; i < v[t].size(); i++) v[t][i].first += v[t][i - 1].first;
	}
	int ans = 0, p[3] = {-1, -1, -1};
	for (int i = -1; i < v[2].size(); i++) {
		int rem = vol - 2 * (i + 1);
		if (rem < 0) break;
		rem = min(rem, (int)v[1].size());
		int cur = (i == -1 ? 0 : v[2][i].first) + (rem == 0 ? 0 : v[1][rem - 1].first);
		if (ans < cur) ans = cur, p[2] = i, p[1] = rem - 1;
	}
	printf("%d\n", ans);
	for (int t = 1; t <= 2; t++)
		for (int i = 0; i <= p[t]; i++) ret.push_back(v[t][i].second);//printf("%d ", v[t][i].second);
	sort(ret.begin(), ret.end());
	for (int i = 0; i < ret.size(); i++) printf("%d ", ret[i]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define sf(x)       scanf("%d",&x)
struct node{
    int no;
    int pos;
};
bool myfunc(node a, node b)
{
    return a.no>b.no;
}
void printQ(vector<node>vec)
{
    for(vector<node>::iterator it1=vec.begin();it1!=vec.end();it1++)cout<<it1->no<<' '<<it1->pos<<endl;
    return;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.cpp","r",stdin);
    freopen("output.cpp","w",stdout);
    #endif // ONLINE_JUDGE
    int t,n,v,c;
    vector<node>ky,ct;
    set<int> ans;
    sf(t);
    sf(v);
    for(int i=0;i<t;i++)
    {
        sf(n);
        sf(c);
        if(n==1)ky.push_back({c,i+1});
        else ct.push_back({c,i+1});
    }
    int num1=ky.size(),num2=ct.size(),pos1=0,pos2=0,vol=v,sum=0;
    sort(ky.begin(),ky.end(),myfunc);
    sort(ct.begin(),ct.end(),myfunc);
    //printQ(ky);
    //printQ(ct);
    for(pos2=0;pos2<num2 && vol>1;pos2++)
    {
        ans.insert(ct[pos2].pos);
        vol-=2;
        sum+=ct[pos2].no;
    }
    for(pos2;pos1<num1 && vol>0;pos1++)
    {
        ans.insert(ky[pos1].pos);
        sum+=ky[pos1].no;
        v--;
    }
    for(pos2--;pos2>=0;pos2--)
    {
        if(pos1>=num1)break;
        if(pos1<num1-1)
        {
            if(ky[pos1].no+ky[pos1].no>ct[pos2].no)
            {
                ans.erase(ct[pos2].pos);
                ans.insert(ky[pos1].pos);
                ans.insert(ky[pos1+1].pos);
                sum+=(ky[pos1].no+ky[pos1+1].no)-ct[pos2].no;
                pos1+=2;
            }
            else break;
        }
        else if(ky[pos1].no>ct[pos2].no)
        {
            ans.erase(ct[pos2].pos);
            ans.insert(ky[pos1].pos);
            sum+=ky[pos1].no-ct[pos2].no;
            pos1++;
        }
    }
    cout<<sum<<endl;
    for(set<int>::iterator it=ans.begin();it!=ans.end();it++)cout<<*it<<' ';
    cout<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define sf(x)       scanf("%d",&x)
struct node{
    int no;
    int pos;
};
bool myfunc(node a, node b)
{
    return a.no>b.no;
}
void printQ(vector<node>vec)
{
    for(vector<node>::iterator it1=vec.begin();it1!=vec.end();it1++)cout<<it1->no<<' '<<it1->pos<<endl;
    return;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.cpp","r",stdin);
    freopen("output.cpp","w",stdout);
    #endif // ONLINE_JUDGE
    int t,n,v,c;
    vector<node>ky,ct;
    set<int> ans;
    sf(t);
    sf(v);
    for(int i=0;i<t;i++)
    {
        sf(n);
        sf(c);
        if(n==1)ky.push_back({c,i+1});
        else ct.push_back({c,i+1});
    }
    int num1=ky.size(),num2=ct.size(),pos1=0,pos2=0,vol=v,sum=0;
    sort(ky.begin(),ky.end(),myfunc);
    sort(ct.begin(),ct.end(),myfunc);
    //printQ(ky);
    //printQ(ct);
    for(pos2=0;pos2<num2 && vol>1;pos2++)
    {
        ans.insert(ct[pos2].pos);
        vol-=2;
        sum+=ct[pos2].no;
    }
    for(pos2;pos1<num1 && vol>0;pos1++)
    {
        ans.insert(ky[pos1].pos);
        sum+=ky[pos1].no;
        vol--;
    }
    for(pos2--;pos2>=0;pos2--)
    {
        if(pos1>=num1)break;
        if(pos1<num1-1)
        {
            if(ky[pos1].no+ky[pos1].no>ct[pos2].no)
            {
                ans.erase(ct[pos2].pos);
                ans.insert(ky[pos1].pos);
                ans.insert(ky[pos1+1].pos);
                sum+=(ky[pos1].no+ky[pos1+1].no)-ct[pos2].no;
                pos1+=2;
            }
            else break;
        }
        else if(ky[pos1].no>ct[pos2].no)
        {
            ans.erase(ct[pos2].pos);
            ans.insert(ky[pos1].pos);
            sum+=ky[pos1].no-ct[pos2].no;
            pos1++;
        }
    }
    cout<<sum<<endl;
    for(set<int>::iterator it=ans.begin();it!=ans.end();it++)cout<<*it<<' ';
    cout<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif // ONLINE_JUDGE
    int n,v,maxi=numeric_limits<int>::min();
    cin>>n>>v;
    vector<pair<int,int>> vec(n);
    vector<pair<int,int>>::iterator no;
    for(int i=0;i<n;i++)cin>>vec[i].first>>vec[i].second;
    for(auto i=vec.begin();i!=vec.end();i++)
    {
        if(i->first==1)
        {
            if(maxi<v*i->second*2)
            {
                no=i;
                maxi=v*i->second*2;
            }
        }
        else
        {
            if(maxi<v*i->second*2)
            {
                no=i;
                maxi=v*i->second*2;
            }
        }
    }
    cout<<no->second<<endl<<no->first<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool func(const pair<pair<int,int>,pair<double,bool>> &x, const pair<pair<int,int>,pair<double,bool>> &y)
{
    return (x.second.first>y.second.first);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif // ONLINE_JUDGE
    int n,v,sum=0;
    cin>>n>>v;
    vector<pair<pair<int,int>,pair<double,bool>>> vec(n);
    vector<pair<pair<int,int>,pair<double,bool>>>::iterator no;
    vector<pair<pair<int,int>,pair<double,bool>>> pai(n);
    for(int i=0;i<n;i++)cin>>vec[i].first.first>>vec[i].first.second;
    for(no=vec.begin();no!=vec.end();no++)
    {
        no->second.first=(double)no->first.second/no->first.first;
    }
    sort(vec.begin(),vec.end(),func);
    int i=0;
    for(no=vec.begin();no!=vec.end();no++)
    {
        if(!no->second.second && sum+(no->first.first)<=v)
        {
            pai[i].first.first=no->first.first;
            pai[i].first.second=no->first.second;
            pai[i].second.first=no->second.first;
            pai[i].second.second=no->second.second;
            no->second.second=true;
            sum+=(no->first.first);
            i++;
        }
    }
    sum=0;
    for(no=pai.begin();no!=pai.end();no++)
    {
        sum+=no->first.second;
    }
    cout<<sum<<endl;
    for(int a=0;a<i;a++)cout<<pai[a].first.first<<' ';
    cout<<endl;
    /*for(int i=0;i<n;i++)cout<<vec[i].first.first<<' '<<vec[i].first.second<<' '<<vec[i].second.first<<' '<<vec[i].second.second<<endl;
    cout<<"end"<<endl;
    for(auto a=pai.begin();a!=pai.end();a++)cout<<a->first.first<<' '<<a->first.second<<' '<<a->second.first<<' '<<a->second.second<<endl;*/
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct Lorry
{
    int type;
    int cap;
    double pri;
    bool av=true;
    int rnk;
};
bool func(const Lorry &x,const Lorry &y)
{
    return x.pri>y.pri;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif // ONLINE_JUDGE
    int n,v,sum=0,lorry=0;
    cin>>n>>v;
    vector<Lorry> vec(n);
    vector<int> pos;
    for(int i=0; i<n; i++)
    {
        cin>>vec[i].type>>vec[i].cap;
        vec[i].pri=(double)vec[i].cap/vec[i].type;
        vec[i].rnk=i+1;
    }
    sort(vec.begin(),vec.end(),func);
    for(int j=0; j<n; j++)
    {
        for(auto i=vec.begin(); i!=vec.end(); i++)
        {
            if(i->av && sum+i->type<=v)
            {
                sum+=i->type;
                i->av=false;
                pos.push_back(i->rnk);
                lorry+=i->cap;
                break;
            }
        }
    }
    cout<<lorry<<endl;
    for(auto i=pos.begin();i!=pos.end();i++)cout<<*i<<' ';
    cout<<endl;
    //for(int i=0; i<n; i++)cout<<vec[i].type<<' '<<vec[i].cap<<' '<<vec[i].pri<<' '<<vec[i].av<< ' '<<vec[i].rnk<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef vector< ll > vl;
typedef vector< vl > vvl;
typedef pair< int, int > pii;
typedef vector< pii > vp;
typedef vector< double > vd;
typedef vector< vd > vvd;
typedef vector< string > vs;

template< class T1, class T2 >
int upmin( T1 &x, T2 v ){
  if( x > v ){
    x = v;
    return 1;
  }
  return 0;
}

template< class T1, class T2 >
int upmax( T1 &x, T2 v ){
  if( x < v ){
    x = v;
    return 1;
  }
  return 0;
}

const int INF = 0x3f3f3f3f;

int N, V;
vi T, P;

void init(){
  cin >> N >> V;
  T = P = vi( N );
  for( int i = 0; i < N; ++i ){
    cin >> T[ i ] >> P[ i ];
  }
}

vector< pair< double, int > > obj;
vi choice;

void preprocess(){
  for( int i = 0; i < N; ++i ){
    obj.emplace_back( 1.0 * P[ i ] / 2, i );
  }
  sort( obj.begin(), obj.end(), greater< pair< double, int > >() );
  ll ans = 0;
  for( int i = 0; i < obj.size(); ++i ){
    if( V <= 2 ){
      if( V == 1 ){
        for( int j = i; j < obj.size(); ++j ){
          if( T[ obj[ j ].second ] == 1 ){
            ans += P[ obj[ j ].second ];
            choice.emplace_back( j );
            break;
          }
        }
      } else if( V == 2 ){
        int maxv = 0;
        int dc = -1, a = -1, b = -1;
        for( int j = i; j < obj.size(); ++j ){
          if( T[ obj[ j ].second ] == 1 ){
            dc = 0;
            a = obj[ j ].second;
            int maxv2 = 0;
            for( int k = j + 1; k < obj.size(); ++k ){
              if( T[ obj[ k ].second ] == 1 ){
                upmax( maxv2, P[ obj[ k ].second ] );
                b = obj[ k ].second;
                break;
              }
            }
            upmax( maxv, P[ obj[ j ].second ] + maxv2 );
          }
        }
        for( int j = i; j < obj.size(); ++j ){
          if( T[ obj[ j ].second ] == 2 ){
            if( upmax( maxv, P[ obj[ j ].second ] ) ){
              dc = 1;
              a = obj[ j ].second;
            }
            break;
          }
        }
        ans += maxv;
        if( dc == 0 ){
          choice.emplace_back( a );
          if( b != -1 ){
            choice.emplace_back( b );
          }
        } else if( dc == 1 ){
          choice.emplace_back( a );
        }
      }
      break;
    }
    choice.emplace_back( i );
    V -= obj[ i ].second;
    V += obj[ i ].first;
  }
  cout << ans << endl;
  for( int i = 0; i < choice.size(); ++i ){
    cout << choice[ i ] + 1 << " \n"[ i + 1 == choice.size() ];
  }
}

void solve(){

}

signed main(){
  ios::sync_with_stdio( 0 );
  init();
  preprocess();
  solve();
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef vector< ll > vl;
typedef vector< vl > vvl;
typedef pair< int, int > pii;
typedef vector< pii > vp;
typedef vector< double > vd;
typedef vector< vd > vvd;
typedef vector< string > vs;

template< class T1, class T2 >
int upmin( T1 &x, T2 v ){
  if( x > v ){
    x = v;
    return 1;
  }
  return 0;
}

template< class T1, class T2 >
int upmax( T1 &x, T2 v ){
  if( x < v ){
    x = v;
    return 1;
  }
  return 0;
}

const int INF = 0x3f3f3f3f;

int N, V;
vi T, P;

void init(){
  cin >> N >> V;
  T = P = vi( N );
  for( int i = 0; i < N; ++i ){
    cin >> T[ i ] >> P[ i ];
  }
}

ll ans;
int best2;

void preprocess(){
  priority_queue< pii > one, two;
  for( int i = 0; i < N; ++i ){
    if( T[ i ] == 1 ){
      one.emplace( P[ i ], i );
    } else{
      two.emplace( P[ i ], i );
    }
  }
  ll two_sum = 0;
  ll one_sum = 0;
  int one_cnt = 0;
  priority_queue< pii, vector< pii >, greater< pii > > one_back;
  for( int i = 0; i < V; ++i ){
    if( one.empty() ){
      break;
    }
    ++one_cnt;
    one_back.emplace( one.top() );
    one_sum += one.top().first; one.pop();
  }
  upmax( ans, one_sum );
  for( int i = 1; not two.empty(); ++i ){
    while( one_cnt and one_cnt + i * 2 > V ){
      --one_cnt;
      one_sum -= one_back.top().first; one_back.pop();
    }
    if( i * 2 > V ){
      break;
    }
    two_sum += two.top().first; two.pop();
    if( upmax( ans, one_sum + two_sum ) ){
      best2 = i;
    }
  }
}

void solve(){
  cout << ans << endl;
  priority_queue< pii > one, two;
  for( int i = 0; i < N; ++i ){
    if( T[ i ] == 1 ){
      one.emplace( P[ i ], i );
    } else{
      two.emplace( P[ i ], i );
    }
  }
  vi id;
  for( int i = 0; i < best2; ++i ){
    id.emplace_back( two.top().second );
    two.pop();
  }
  // cout << V - 2 * best2 << endl;
  for( int i = 0, t = one.size(); i < min( V - 2 * best2, t ); ++i ){
    id.emplace_back( one.top().second );
    one.pop();
  }
  for( int i = 0; i < id.size(); ++i ){
    cout << id[ i ] + 1 << " \n"[ i + 1 == id.size() ];
  }
}

signed main(){
  ios::sync_with_stdio( 0 );
  init();
  preprocess();
  solve();
  return 0;
}


#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define y1 y11
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
//#define mp make_pair
#define pb push_back
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define plii pair<LL,pii>
#define pii pair<int, int>
#define piii pair<pii, pii>
#define pdi pair<double, int>
#define pdd pair<double, double>
#define mem(a, b) memset(a, b, sizeof(a))
#define debug(x) cerr << #x << " = " << x << "\n";
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//head

const int N = 1e5 + 5;
char s[N];
int pre[26], nxt[26], n;
bool vis[26], vv[26];
int main() {
	mem(pre, -1), mem(nxt, -1);
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%s", s);
		int l = strlen(s);
		for (int j = 0; j < l; ++j) vis[s[j]-'a'] = true;
		for (int j = 0; j+1 < l; ++j) {
			int x = s[j]-'a', y = s[j+1]-'a';
			if(nxt[x] != -1 && nxt[x] != y) return 0, puts("NO");
			if(pre[y] != -1 && pre[y] != x) return 0, puts("NO");
			nxt[x] = y;
			pre[y] = x;
		}
	}
	for (int i = 0; i < 26; ++i) {
		if(vis[i] && pre[i] == -1) {
			int x = i;
			while(true) {
				vv[x] = true;
				x = nxt[x];
				if(x == -1) break;
			}
		}
	}
	for (int i = 0; i < 26; ++i) if(vis[i] && !vv[i]) return 0, puts("NO");
	for (int i = 0; i < 26; ++i) {
		if(vis[i] && pre[i] == -1) {
			int x = i;
			while(true) {
				putchar('a'+x);
				x = nxt[x];
				if(x == -1) break;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define _for(i,a,b) for(int i=(a);i<(b);++i)
#define ms(a) memset(a,0,sizeof(a))
#define ms_inf(a) memset(a,0x3f,sizeof(a))
#define ms_ninf(a) memset(a,0xcf,sizeof(a))
#define pb push_back
#define mp make_pair
#define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define lc (o<<1)
#define rc (o<<1|1)
#define mid ((l+r)>>1)
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
mt19937_64 rng((ull)new char);
const double eps = 1e-7;
const int maxn = 1e6+100;
const ll INF = 9e8;
const int mod=1e9+7;
bool g[26][26];
string s[maxn];
int n;
bool vis[26];
bool done[26];
bool dfs(int u)
{

	done[u]=1;
	_for(t,0,26)
	{
		if(g[u][t])
		{
			if(done[t])return 0;
			if(!dfs(t))return 0;
		}
	}
	return 1;
}
string ans;
void dfs2(int u)
{
	done[u]=1;
	ans+=u+'a';
	_for(t,0,26)
	{
		if(g[u][t])
		{
			dfs2(t);
		}
	}
}
int main()
{
	fast;cin>>n;
	_for(t,0,n)
	{
		cin>>s[t];
		_for(i,0,s[t].length()-1)
		{
			g[s[t][i]-'a'][s[t][i+1]-'a']=1;
		}
		for(auto&c:s[t])vis[c-'a']=1;
	}
	bool fl=1;
	_for(t,0,26)
	{
		int cnt=0;
		_for(j,0,26)cnt+=g[t][j];
		if(cnt>=2)
		{
			fl=0;break;
		}
	}
	_for(t,0,26)
	{
		int cnt=0;
		_for(j,0,26)cnt+=g[j][t];
		if(cnt>=2)
		{
			fl=0;break;
		}
	}
	_for(t,0,26)if(vis[t])
	{
		ms(done);
		if(!dfs(t))
		{
			fl=0;break;
		}
	}
	if(fl)
	{
		ms(done);
		int in[26];ms(in);
		_for(t,0,26)
		{
			_for(i,0,26)in[t]+=g[i][t];
		}
		_for(t,0,26)
		{
			if(vis[t]&&!done[t]&&!in[t])
			{
				dfs2(t);
			}
		}
		cout<<ans;
	}
	else cout<<"NO";
}


#include<bits/stdc++.h>
using namespace std;
#define _for(i,a,b) for(int i=(a);i<(b);++i)
#define ms(a) memset(a,0,sizeof(a))
#define ms_inf(a) memset(a,0x3f,sizeof(a))
#define ms_ninf(a) memset(a,0xcf,sizeof(a))
#define pb push_back
#define mp make_pair
#define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define lc (o<<1)
#define rc (o<<1|1)
#define mid ((l+r)>>1)
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
mt19937_64 rng((ull)new char);
const double eps = 1e-7;
const int maxn = 1e6+100;
const ll INF = 9e8;
const int mod=1e9+7;
bool g[26][26];
string s[maxn];
int n;
bool vis[26];
bool done[26];
bool dfs(int u)
{

	done[u]=1;
	_for(t,0,26)
	{
		if(g[u][t])
		{
			if(done[t])return 0;
			if(!dfs(t))return 0;
		}
	}
	return 1;
}
string ans;
void dfs2(int u)
{
	done[u]=1;
	ans+=u+'a';
	_for(t,0,26)
	{
		if(g[u][t])
		{
			dfs2(t);
		}
	}
}
int main()
{
	fast;cin>>n;
	_for(t,0,n)
	{
		cin>>s[t];
		_for(i,0,s[t].length()-1)
		{
			g[s[t][i]-'a'][s[t][i+1]-'a']=1;
		}
		for(auto&c:s[t])vis[c-'a']=1;
	}
	bool fl=1;
	_for(t,0,26)
	{
		int cnt=0;
		_for(j,0,26)cnt+=g[t][j];
		if(cnt>=2)
		{
			fl=0;break;
		}
	}
	_for(t,0,26)if(vis[t])
	{
		ms(done);
		if(!dfs(t))
		{
			fl=0;break;
		}
	}
	if(fl)
	{
		ms(done);
		int in[26];ms(in);
		_for(t,0,26)
		{
			_for(i,0,26)in[t]+=g[i][t];
		}
		_for(t,0,26)
		{
			if(vis[t]&&!done[t]&&!in[t])
			{
				dfs2(t);
			}
		}
		cout<<ans;
	}
	else cout<<"NO";
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
string ss[maxn];
int b[maxn];
int num[maxn];
int check(string aa,string bb){
    for(int i=0;i<aa.size();i++){
        if(bb[0]==aa[i]){
            int k=0; int flag=0;
            while(k<bb.size()  && i+k<aa.size()) {
                if(bb[k]!=aa[i+k]) flag=1; k++;
            }
            if(flag==0) return 1;
        }
    }
    swap(aa,bb);
    for(int i=0;i<aa.size();i++){
        if(bb[0]==aa[i]){
            int k=0; int flag=0;
            while(k<bb.size()  && i+k<aa.size()) {
                if(bb[k]!=aa[i+k]) flag=1; k++;
            }
            if(flag==0) return 1;
        }
    }
    return 0;
}
string make(string aa,string bb){
    string xx="";
    for(int i=0;i<aa.size();i++){
        if(bb[0]==aa[i]){
            int k=0; int flag=0;
            while(k<bb.size()  && i+k<aa.size()) {
                if(bb[k]!=aa[i+k]) flag=1; k++;
            }
            if(flag==0){
                xx=aa;
                while(k<bb.size()){
                    xx+=bb[k]; k++;
                }
                return xx;
            }
        }
    }
    swap(aa,bb);
    for(int i=0;i<aa.size();i++){
        if(bb[0]==aa[i]){
            int k=0; int flag=0;
            while(k<bb.size()  && i+k<aa.size()) {
                if(bb[k]!=aa[i+k]) flag=1; k++;
            }
            if(flag==0){
                xx=aa;
                while(k<bb.size()){
                    xx+=bb[k]; k++;
                }
                return xx;
            }
        }
    }
}
int main(){
    int n; cin>>n;
    for(int i=1;i<=n;i++) cin>>ss[i];  // mo ren 26
    int flag=0;
    if(n>=900) flag=1;
    for(int i=1;i<=n;i++){
        if(ss[i].size()>26) flag=1;
        for(int j=0;j<=26;j++) num[j]=0;
        for(int j=0;j<ss[i].size();j++){
            if(num[ss[i][j]-'a']==1) flag=1;
            num[ss[i][j]-'a']++;
        }
    }
    vector<string> vs;
    if(flag==1) {cout<<"NO"<<endl; return 0; }
    for(int i=1;i<=n;i++){
        if(b[i]==1) continue;
        for(int j=1;j<=n;j++){
            if( check(ss[i],ss[j]) && i!=j ){
                b[j]=1; ss[i]=make(ss[i],ss[j]);
            }
        }
    }
    for(int i=1;i<=n;i++){
        if(b[i]==0) vs.push_back(ss[i]);
    }
    for(int i=0;i<vs.size();i++){
        for(int j=i+1;j<vs.size();j++){
            if(vs[i]>vs[j]) swap(vs[i],vs[j]);
        }
    }
    string ans="";
    for(int i=0;i<vs.size();i++) ans+=vs[i];
    for(int i=0;i<=26;i++) num[i]=0;
    for(int i=0;i<ans.size();i++){
        if(num[ans[i]-'a']) flag=1;
        num[ans[i]-'a']++;
    }
    if(flag) cout<<"NO"<<endl;
    else cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
string ss[maxn];
int b[maxn];
int num[maxn];
int check(string aa,string bb){
    for(int i=0;i<aa.size();i++){
        if(bb[0]==aa[i]){
            int k=0; int flag=0;
            while(k<bb.size()  && i+k<aa.size()) {
                if(bb[k]!=aa[i+k]) flag=1; k++;
            }
            if(flag==0) return 1;
        }
    }
    swap(aa,bb);
    for(int i=0;i<aa.size();i++){
        if(bb[0]==aa[i]){
            int k=0; int flag=0;
            while(k<bb.size()  && i+k<aa.size()) {
                if(bb[k]!=aa[i+k]) flag=1; k++;
            }
            if(flag==0) return 1;
        }
    }
    return 0;
}
string make(string aa,string bb){
    string xx="";
    for(int i=0;i<aa.size();i++){
        if(bb[0]==aa[i]){
            int k=0; int flag=0;
            while(k<bb.size()  && i+k<aa.size()) {
                if(bb[k]!=aa[i+k]) flag=1; k++;
            }
            if(flag==0){
                xx=aa;
                while(k<bb.size()){
                    xx+=bb[k]; k++;
                }
                return xx;
            }
        }
    }
    swap(aa,bb);
    for(int i=0;i<aa.size();i++){
        if(bb[0]==aa[i]){
            int k=0; int flag=0;
            while(k<bb.size()  && i+k<aa.size()) {
                if(bb[k]!=aa[i+k]) flag=1; k++;
            }
            if(flag==0){
                xx=aa;
                while(k<bb.size()){
                    xx+=bb[k]; k++;
                }
                return xx;
            }
        }
    }
}
int main(){
    int n; cin>>n;
    for(int i=1;i<=n;i++) cin>>ss[i];  // mo ren 26
    int flag=0;
    if(n>=900) flag=1;
    for(int i=1;i<=n;i++){
        if(ss[i].size()>26) flag=1;
        for(int j=0;j<=26;j++) num[j]=0;
        for(int j=0;j<ss[i].size();j++){
            if(num[ss[i][j]-'a']==1) flag=1;
            num[ss[i][j]-'a']++;
        }
    }
    vector<string> vs;
    if(flag==1) {cout<<"NO"<<endl; return 0; }
    for(int i=1;i<=n;i++){
        if(b[i]==1) continue;
        string cc=ss[i];
        for(int j=i+1;j<=n;j++){
            if( check(cc,ss[j]) ){
                b[j]=1; cc=make(cc,ss[j]);
            }
        }
        vs.push_back(cc);
        //cout<<cc<<endl;
    }
    for(int i=0;i<vs.size();i++){
        for(int j=i+1;j<vs.size();j++){
            if(vs[i]>vs[j]) swap(vs[i],vs[j]);
        }
    }
    string ans="";
    for(int i=0;i<vs.size();i++) ans+=vs[i];
    for(int i=0;i<=26;i++) num[i]=0;
    for(int i=0;i<ans.size();i++){
        if(num[ans[i]-'a']) flag=1;
        num[ans[i]-'a']++;
    }
    if(flag) cout<<"NO"<<endl;
    else cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define dbg(x) cerr << #x << " = " << x << endl
#define _ << ' ' <<
using namespace std;

void no() {
  cout << "NO\n";
  exit(0);
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n; cin >> n;
  set<char> has;
  map<char, char> adj;
  map<char, int> deg, vis;
  for (int i = 0; i < n; ++i) {
    string str; cin >> str;
    for (int i = 0; str[i]; ++i) {
      if (str[i + 1]) {
        if (adj[str[i]] && adj[str[i]] != str[i + 1]) no();
        adj[str[i]] = str[i + 1];
        deg[str[i + 1]]++;
      }
      has.insert(str[i]);
    }
  }
  string sol;
  function<void(char)> dfs = [&] (char c) {
    if (vis[c]) no();
    sol.push_back(c);
    vis[c] = 1;
    if (adj[c]) dfs(adj[c]);
  };
  bool flag = true;
  for (char c : has) {
    dfs(c);
  }
  if (vis.size() != has.size()) no();
  cout << sol << '\n';
}


#include <bits/stdc++.h>
#define dbg(x) cerr << #x << " = " << x << endl
#define _ << ' ' <<
using namespace std;

void no() {
  cout << "NO\n";
  exit(0);
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n; cin >> n;
  set<char> has;
  map<char, char> adj;
  map<char, int> deg, vis;
  for (int i = 0; i < n; ++i) {
    string str; cin >> str;
    for (int i = 0; str[i]; ++i) {
      if (str[i + 1]) {
        if (adj[str[i]] && adj[str[i]] != str[i + 1]) no();
        adj[str[i]] = str[i + 1];
        deg[str[i + 1]]++;
      }
      has.insert(str[i]);
    }
  }
  string sol;
  function<void(char)> dfs = [&] (char c) {
    if (vis[c]) no();
    sol.push_back(c);
    vis[c] = 1;
    if (adj[c]) dfs(adj[c]);
  };
  for (char c : has) {
    if (!deg[c]) dfs(c);
  }

  cout << sol << '\n';
}


#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

map <char, int> used, cnt;
map <char, char> next_ch;
string ans;

bool flag = false;

void dfs(char c) {
    if (used[c]) {
        flag = true;
        return;
    }

    ans.push_back(c);
    used[c] = 1;
    if (next_ch[c] )dfs(next_ch[c]);

    return;
}

int main() {
    ios::sync_with_stdio(0);
    int n; cin >> n;

    set <char> has;

    for (int i = 0; i < n; i++) {
        string s; cin >> s;
        int len = s.size();

        for (int i = 0; i < len; i++) {
            if (i != len - 1) {
                char c = s[i];
                if (next_ch[c] && next_ch[c] != s[i + 1]) {
                    cout << "NO";
                    return 0;
                }

                next_ch[c] = s[i + 1];
                cnt[s[i + 1]]++;
            }

            has.insert(s[0]);
        }
    }

    set <char> :: iterator it = has.begin();

    for (; it != has.end(); it++) {
        if (!cnt[*it]) {
            dfs(*it);

            if (flag) {
                cout << "NO";
                return 0;
            }
        }
    }
    //cout << used.size() << " " << cnt.size();
    if (used.size() != cnt.size()) {
        cout << "NO";
        return 0;
    }

    cout << ans;

    return 0;
}


#include <bits/stdc++.h>
#define dbg(x) cerr << #x << " = " << x << endl
#define _ << ' ' <<
using namespace std;

void no() {
  cout << "NO\n";
  exit(0);
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n; cin >> n;
  set<char> has;
  map<char, char> adj;
  map<char, int> deg, vis;
  for (int i = 0; i < n; ++i) {
    string str; cin >> str;
    for (int i = 0; str[i]; ++i) {
      if (str[i + 1]) {
        if (adj[str[i]] && adj[str[i]] != str[i + 1]) no();
        adj[str[i]] = str[i + 1];
        deg[str[i + 1]]++;
      }
      has.insert(str[i]);
    }
  }
  string sol;
  function<void(char)> dfs = [&] (char c) {
    if (vis[c]) no();
    sol.push_back(c);
    vis[c] = 1;
    if (adj[c]) dfs(adj[c]);
  };
  bool flag = true;
  for (char c : has) {
    if (!deg[c]) dfs(c), flag = false;
  }
  if (flag) no();
  cout << sol << '\n';
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
typedef long double ld;
typedef pair<ll,ll> pll;
const ld pi=3.14159265359;
const ll N=3e5+5;
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(a) (ll)a.size()

ll n,ok=1,x;
string s,tmp;
ll u[26][26],vis[26],d[26],h[26];
vector<ll> v[26];

void DFS(ll w){
	vis[w]=2;
	for(auto i:v[w]){
		if(vis[i]==2)ok=0;
		if(!vis[i])DFS(i);
	}
	vis[w]=1;
}

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin>>n;
	while(n--){
		cin>>s;
		REP(i,SZ(s)-1)u[s[i]-'a'][s[i+1]-'a']=1,h[s[i]-'a']=h[s[i+1]-'a']=1;
	}
	REP(i,26)REP(j,26)if(u[i][j])v[i].pb(j),d[j]++;
	REP(i,26){
		if(vis[i]==0)DFS(i);
		if(d[i]>1||SZ(v[i])>1)ok=0;
	}
	if(!ok){cout<<"NO\n";return 0;}
	memset(vis,0,sizeof(vis));
	REP(i,26){
		if(d[i]==0&&h[i]){
			x=i;
			while(SZ(v[x]))tmp.pb(x+'a'),x=v[x][0];
			tmp.pb(x+'a');
		}
	}
	cout<<tmp<<"\n";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
typedef long double ld;
typedef pair<ll,ll> pll;
const ld pi=3.14159265359;
const ll N=3e5+5;
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(a) (ll)a.size()

ll n,ok=1,x;
string s,tmp;
ll u[26][26],vis[26],d[26],h[26];
vector<ll> v[26];

void DFS(ll w){
	vis[w]=2;
	for(auto i:v[w]){
		if(vis[i]==2)ok=0;
		if(!vis[i])DFS(i);
	}
	vis[w]=1;
}

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin>>n;
	while(n--){
		cin>>s;
		h[s[0]-'a']=1;
		REP(i,SZ(s)-1)u[s[i]-'a'][s[i+1]-'a']=1,h[s[i+1]-'a']=1;
	}
	REP(i,26)REP(j,26)if(u[i][j])v[i].pb(j),d[j]++;
	REP(i,26){
		if(vis[i]==0)DFS(i);
		if(d[i]>1||SZ(v[i])>1)ok=0;
	}
	if(!ok){cout<<"NO\n";return 0;}
	memset(vis,0,sizeof(vis));
	REP(i,26){
		if(d[i]==0&&h[i]){
			x=i;
			while(SZ(v[x]))tmp.pb(x+'a'),x=v[x][0];
			tmp.pb(x+'a');
		}
	}
	cout<<tmp<<"\n";
	return 0;
}