#include<bits/stdc++.h>
#include<algorithm>
#include<string>
#include<cmath>

using namespace std;

long long int arr1[200005];
long long int remainders[500000001];
//char arr[100005];

int main(void)
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    long long int t;
    cin >> t;
    while(t--)
    {
        for(long long int i=0; i<500000001;i++)
        {
            remainders[i]=0;
        }
        long long int n,x;
        cin >> n;
        for(long long int i=0; i < n ; i++)
        {
            cin >> x;
            arr1[i] = x;
           // arr2[i] = x;
        }
    sort(arr1,arr1+n, greater<int>());
    // for(int i=0; i< n; i++)
    // {
    //     cout << arr1[i] << " ";
    // }
    // cout << endl;
    long long int ans=0,answer=0;
    long long int answer_array[200005],k=1;
    for(long long int i=0; i< n; i++)
    {
        ans=0;
        long long int num = arr1[i],final;
        if(arr1[i]%2==0)
        {
            while(num%2 == 0)
            {
                num = num/2;
                ans+=1;
                final = num;
            }
            // cout << "ans " << ans << endl;
            // cout <<"final " << final << endl;
            arr1[i] = final;
            //remainders[final] = 1;
            if(remainders[final]!=1 && i!=1)
            {
                remainders[final] = 1;
                answer_array[k++]=ans;
            }
            // for(int i=0; i< n; i++)
            // {
            //     cout << arr1[i] << " ";
            // }
            // cout << endl;
            // cout << "answer"  << answer << endl;
        }
    }
    long long int sum = 0;
    for(long long int i=1; i<k; i++)
    {
        sum+=answer_array[i];
    }
    cout << sum << endl;
    }
    return 0;
}

// for(long long int i=1; i<= ; i++)
// {
// }


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long int n,ans=0,x,i;
		set<long long int>s;
		cin>>n;
		for(i=0;i<n;i++)
		{
			cin>>x;
			s.insert(x);
		}
		while(!s.empty())
		{
			x=*s.begin();
			s.erase(x);
			if(x%2==0)
			{
				ans++;
				s.insert(x/2);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long int n,ans=0,x,i;
		set<long long int>s;
		cin>>n;
		for(i=0;i<n;i++)
		{
			cin>>x;
			s.insert(x);
		}
		while(!s.empty())
		{
			x=*s.rbegin();
			s.erase(x);
			if(x%2==0)
			{
				ans++;
				s.insert(x/2);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long int n,ans=0,x,i;
		set<long long int>s;
		cin>>n;
		for(i=0;i<n;i++)
		{
			cin>>x;
			s.insert(x);
		}
		while(!s.empty())
		{
			x=s.begin();
			s.erase(x);
			if(x%2==0)
			{
				ans++;
				s.insert(x/2);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long int n,x,i,ans=0,c=0,j;
		map<long long int,long long int>m;
		map<long long int,long long int>::iterator itr;
		priority_queue<long long int>q;
		cin>>n;
		for(i=0;i<n;i++)
		{
			cin>>x;
			if(x%2==0 && !m[x])
				m[x]++;
		}
		if(m.size()>0)
		{
			long long int a[m.size()];
			i=0;
			for(itr=m.begin();itr!=m.end();itr++)
				a[i++]=itr->first;
			for(i=m.size()-1;i>=0;i--)
			{
				x=a[i];
				while(x%2==0 && x)
				{
					x/=2;
					ans++;
					if(x%2==0 && i>0)
					{
//						cout<<1<<endl;
						for(j=0;j<i;j++)
						{
							if(a[j]==x)
							{
//								cout<<2<<endl;
								c++;
								a[j]=0;
								break;
							}
						}
					}
//					cout<<x<<" "<<c<<endl;
				}
				if(c==m.size()-i-1)
					break;
				c=0;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxx = 2e5 + 7;
int cas, n, a[maxx];
int even[maxx];
map <int, bool> vis;
queue <int> qua;

int main() {
	cin >> cas;
	while(cas--) {
		while(!qua.empty()) qua.pop();
		int ans = 0;
		vis.clear();
		int cnt = 0;
		cin >> n;
		for(int i = 1; i <= n; i++) {
			cin >> a[i];
			if(a[i] % 2 == 0 && !vis[a[i]]) {
				even[++cnt] = a[i];
				vis[a[i]] = 1;
			}
		}
		sort(even + 1, even + cnt + 1);
		for(int i = 1; i <= cnt; i++) {
			bool chk = 1;
			for(int bit = 1; bit <= 30; bit++) {
				int x = 1 << bit;
				int num = x * even[i];
				if(num > 1e9) continue;
				if(vis[num]) {
					chk = 0;
					break;
				}
			}
			if(chk) qua.push(even[i]);
		}
		while(!qua.empty()) {
			int num = qua.front();
			while(num % 2 == 0) {
				num /= 2;
				ans++;
			}
			qua.pop();
		}
		cout << ans << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxx = 2e5 + 7;
int cas, n, a[maxx];
int even[maxx];
map <int, bool> vis;
queue <int> qua;

int main() {
	cin >> cas;
	while(cas--) {
		while(!qua.empty()) qua.pop();
		int ans = 0;
		vis.clear();
		int cnt = 0;
		cin >> n;
		for(int i = 1; i <= n; i++) {
			cin >> a[i];
			if(a[i] % 2 == 0 && !vis[a[i]]) {
				even[++cnt] = a[i];
				vis[a[i]] = 1;
			}
		}
		sort(even + 1, even + cnt + 1);
		for(int i = 1; i <= cnt; i++) {
			bool chk = 1;
			for(int bit = 1; bit <= 30; bit++) {
				int x = 1 << bit;
				LL num = (LL)x * (LL)even[i];
				if(num > (LL)1e9) continue;
				if(vis[num]) {
					chk = 0;
					break;
				}
			}
			if(chk) qua.push(even[i]);
		}
		while(!qua.empty()) {
			int num = qua.front();
			while(num % 2 == 0) {
				num /= 2;
				ans++;
			}
			qua.pop();
		}
		cout << ans << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxx = 2e5 + 7;
int cas, n, a[maxx];
int even[maxx];
map <int, bool> vis;
queue <int> qua;

int main() {
	cin >> cas;
	while(cas--) {
		while(!qua.empty()) qua.pop();
		int ans = 0;
		vis.clear();
		int cnt = 0;
		cin >> n;
		for(int i = 1; i <= n; i++) {
			cin >> a[i];
			if(a[i] % 2 == 0 && !vis[a[i]]) {
				even[++cnt] = a[i];
				vis[a[i]] = 1;
			}
		}
		sort(even + 1, even + cnt + 1);
		for(int i = 1; i <= cnt; i++) {
			bool chk = 1;
			for(int bit = 1; bit <= 30; bit++) {
				int x = 1 << bit;
				if(vis[x * even[i]]) {
					chk = 0;
					break;
				}
			}
			if(chk) qua.push(even[i]);
		}
		while(!qua.empty()) {
			int num = qua.front();
			while(num % 2 == 0) {
				num /= 2;
				ans++;
			}
			qua.pop();
		}
		cout << ans << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxx = 2e5 + 7;
int cas, n, a[maxx], even[maxx];
map <int, bool> vis;
queue <int> qua;

int main() {
	cin >> cas;
	while(cas--) {
		int ans = 0, cnt = 0;
		vis.clear();
		cin >> n;
		for(int i = 1; i <= n; i++) {
			cin >> a[i];
			if(a[i] % 2 == 0 && !vis[a[i]]) {
				even[++cnt] = a[i];
				vis[a[i]] = 1;
			}
		}
		for(int i = 1; i <= cnt; i++) {
			bool chk = 1;
			for(int bit = 1; bit <= 30; bit++) {
				int x = 1 << bit;
				LL num = (LL)x * (LL)even[i];
				if(num > (LL)1e9) continue;
				if(vis[num]) {
					chk = 0;
					break;
				}
			}
			if(chk) qua.push(even[i]);
		}
		while(!qua.empty()) {
			int num = qua.front();
			while(num % 2 == 0) {
				num /= 2;
				ans++;
			}
			qua.pop();
		}
		cout << ans << endl;
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int  num[200005];
bool bj[1000000005];

int main (){
	int t;
	cin>>t;
	while (t--){
		memset (bj,false,sizeof bj);
		int n;
		cin>>n;
		int sum=0;
		for (int i=0;i<n;i++) cin>>num[i];
		sort (num,num+n);
		for (int i=n-1;~i;i--){
			while (!(num[i]&1)) {
				if (bj[num[i]/2]) break;
				sum++;
				num[i]/=2;
				bj[num[i]]=true;
			}
		}
		cout<<sum<<endl;
	}
}


#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
//int  num[200005];
bool bj[500000005];

int main (){
	int t;
	cin>>t;
	while (t--){
		memset (bj,false,sizeof bj);
		int n;
		cin>>n;
		int sum=0;
		int num;
		for (int i=0;i<n;i++) {
			cin>>num;
			while (!(num&1)) {
				if (bj[num/2]) break;
				sum++;
				num/=2;
				bj[num]=true;
			}
		}
		//sort (num,num+n);
		for (int i=n-1;~i;i--){

		}
		cout<<sum<<endl;
	}
}


#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;

int main (){
	set<int> s;
	int t;
	cin>>t;
	while (t--){
		s.clear();
		int n;
		cin>>n;
		int sum=0;
		int num;
		for (int i=0;i<n;i++) {
			cin>>num;
			//s.insert(num);
			while (!(num&1)) {
				if ((s.count(num/2))!=0) break;
				sum++;
				num/=2;
				s.insert(num);
			}
		}
		//sort (num,num+n);

		cout<<sum<<endl;
	}
}


#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
//int  num[200005];
bool bj[1000000005];

int main (){
	int t;
	cin>>t;
	while (t--){
		memset (bj,false,sizeof bj);
		int n;
		cin>>n;
		int sum=0;
		int num;
		for (int i=0;i<n;i++) {
			cin>>num;
			while (!(num&1)) {
				if (bj[num/2]) break;
				sum++;
				num/=2;
				bj[num]=true;
			}
		}
		//sort (num,num+n);
		for (int i=n-1;~i;i--){

		}
		cout<<sum<<endl;
	}
}


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string.h>

using namespace std;

long long p, t, n, u, s, l;
long long a[300200], c[300200];

int main () {
    cin >> p;
    for ( long long j = 1; j <= p; j++) {
        cin >> n;
        for ( long long i = 1; i <= n; i++) {
           cin >> a[i];
        }  s = 0; t = 0; l = 0; u = 0;
        sort(a + 1, a + 1 + n);
        for ( long long i = 1; i <= n; i++) {
            if ( a[i] % 2 == 0 ) {
              u = a[i];
                while ( a[i] % 2 == 0 ) {
                    l++; c[l] = a[i];
                    a[i] = a[i] / 2;
                }
                //l++; c[l] = a[i];
            }
        }
        sort(c + 1, c + 1 + l);
        //cout << l << endl;
        for (long long i = 1; i <= l; i++) {
            if ( c[i] != c[i - 1]) {
              t++;
            }
            //cout << " i = " << i << " " << c[i] << endl;
        }
        cout << t << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string.h>

using namespace std;

long long p, t, n, u, s, l;
long long a[300200], c[5000500];

int main () {
    cin >> p;
    for ( long long j = 1; j <= p; j++) {
        cin >> n;
        for ( long long i = 1; i <= n; i++) {
           cin >> a[i];
        }  s = 0; t = 0; l = 0; u = 0;
        sort(a + 1, a + 1 + n);
        for ( long long i = 1; i <= n; i++) {
            if ( a[i] % 2 == 0 ) {
              u = a[i];
                while ( a[i] % 2 == 0 ) {
                    l++; c[l] = a[i];
                    a[i] = a[i] / 2;
                }
                //l++; c[l] = a[i];
            }
        }
        sort(c + 1, c + 1 + l);
        //cout << l << endl;
        for (long long i = 1; i <= l; i++) {
            if ( c[i] != c[i - 1]) {
              t++;
            }
            //cout << " i = " << i << " " << c[i] << endl;
        }
        cout << t << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string.h>

using namespace std;

long long p, t, n, u, s, l;
long long a[300200], c[300200];

int main () {
    cin >> p;
    for ( int j = 1; j <= p; j++) {
        cin >> n;
        for ( int i = 1; i <= n; i++) {
           cin >> a[i];
        }  s = 0; t = 0; l = 0; u = 0;
        sort(a + 1, a + 1 + n);
        for ( int i = 1; i <= n; i++) {
            if ( a[i] % 2 == 0 && a[i] != u) {
              u = a[i];
                while ( a[i] % 2 == 0 ) {
                    l++; c[l] = a[i];
                    a[i] = a[i] / 2;
                }
                //l++; c[l] = a[i];
            }
        }
        sort(c + 1, c + 1 + l);
        //cout << l << endl;
        for (int i = 1; i <= l; i++ ) {
            if ( c[i] != c[i - 1]) {
              t++;
            }
            //cout << " i = " << i << " " << c[i] << endl;
        }
        cout << t << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string.h>

using namespace std;

long long p, t, n, u, s, l;
long long a[200200], c[200200];

int main () {
    cin >> p;
    for ( int j = 1; j <= p; j++) {
        cin >> n;
        for ( int i = 1; i <= n; i++) {
           cin >> a[i];
        }  s = 0; t = 0; l = 0; u = 0;
        sort(a + 1, a + 1 + n);
        for ( int i = 1; i <= n; i++) {
            if ( a[i] % 2 == 0 && a[i] != a[i - 1 ]) {
                while ( a[i] % 2 == 0 ) {
                    l++; c[l] = a[i];
                    a[i] = a[i] / 2;
                }
                //l++; c[l] = a[i];
            }
        }
        sort(c + 1, c + 1 + l);
        //cout << l << endl;
        for (int i = 1; i <= l; i++ ) {
            if ( c[i] != c[i - 1]) {
              t++;
            }
            //cout << " i = " << i << " " << c[i] << endl;
        }
        cout << t << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string.h>

using namespace std;

long long p, t, n, u, s, l;
long long a[200200], c[200200];

int main () {
    cin >> p;
    for ( int j = 1; j <= p; j++) {
        cin >> n;
        for ( int i = 1; i <= n; i++) {
           cin >> a[i];
        }  s = 0; t = 0; l = 0; u = 0;
        sort(a + 1, a + 1 + n);
        for ( int i = 1; i <= n; i++) {
            if ( a[i] % 2 == 0 ) {
                while ( a[i] % 2 == 0 ) {
                    l++; c[l] = a[i];
                    a[i] = a[i] / 2;
                }
                //l++; c[l] = a[i];
            }
        }
        sort(c + 1, c + 1 + l);
        //cout << l << endl;
        for (int i = 1; i <= l; i++ ) {
            if ( c[i] != c[i - 1]) {
              t++;
            }
            //cout << " i = " << i << " " << c[i] << endl;
        }
        cout << t << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string.h>

using namespace std;

long long p, t, n, u, s, l;
long long a[300200], c[300200];

int main () {
    cin >> p;
    for ( long long j = 1; j <= p; j++) {
        cin >> n;
        for ( long long i = 1; i <= n; i++) {
           cin >> a[i];
        }  s = 0; t = 0; l = 0; u = 0;
        sort(a + 1, a + 1 + n);
        for ( long long i = 1; i <= n; i++) {
            if ( a[i] % 2 == 0 && a[i] != u) {
              u = a[i];
                while ( a[i] % 2 == 0 ) {
                    l++; c[l] = a[i];
                    a[i] = a[i] / 2;
                }
                //l++; c[l] = a[i];
            }
        }
        sort(c + 1, c + 1 + l);
        //cout << l << endl;
        for (long long i = 1; i <= l; i++) {
            if ( c[i] != c[i - 1]) {
              t++;
            }
            //cout << " i = " << i << " " << c[i] << endl;
        }
        cout << t << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string.h>

using namespace std;

long long p, t, n, u, s, l;
long long a[200200], c[200200];

int main () {
    cin >> p;
    for ( int j = 1; j <= p; j++) {
        cin >> n;
        for ( int i = 1; i <= n; i++) {
           cin >> a[i];
        }  s = 0; t = 0; l = 0; u = 0;
        //sort(a + 1, a + 1 + n);
        for ( int i = 1; i <= n; i++) {
            if ( a[i] % 2 == 0 ) {
                while ( a[i] % 2 == 0 ) {
                    l++; c[l] = a[i];
                    a[i] = a[i] / 2;
                }
                //l++; c[l] = a[i];
            }
        }
        sort(c + 1, c + 1 + l);
        //cout << l << endl;
        for (int i = 1; i <= l; i++ ) {
            if ( c[i] != c[i - 1]) {
              t++;
            }
            //cout << " i = " << i << " " << c[i] << endl;
        }
        cout << t << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string.h>

using namespace std;

long long p, t, n, u, s;
long long a[200200];

int main () {
    cin >> p;
    for ( int j = 1; j <= p; j++) {
        cin >> n;
        for ( int i = 1; i <= n; i++) {
           cin >> a[i];
        }  s = 0; t = 0;
        sort(a + 1, a + 1 + n);
        for ( int i = n; i >= 1; i--) {
            if ( a[i] % 2 == 0 ) {
               //cout << " i = " << i << endl;
                while ( a[i] % 2 == 0 ) {
                t++;
                for ( int z = i - 1; z >= 0; z--) {
                 if ( a[i] == a[z]) {
                     a[z] = a[z] / 2;
                  }
                }
                a[i] = a[i] / 2;
                }
            }
        }
        cout << t << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string.h>

using namespace std;

long long p, t, n, u, s, l;
long long a[200200];

int main () {
    cin >> p;
    for ( int j = 1; j <= p; j++) {
        cin >> n;
        for ( int i = 1; i <= n; i++) {
           cin >> a[i];
        }  s = 0; t = 0; l = 0; u = 0;
        sort(a + 1, a + 1 + n);
        for ( int i = 1; i <= n; i++) {
            if ( a[i] % 2 == 0 ) {
            s = a[i];
                while ( a[i] % 2 == 0 ) {
                    if ( a[i] != u ) {
                        t++; a[i] = a[i] / 2;
                    }
                    else {
                       break;
                    }
                }
                u = s;
            }
        }
        cout << t << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
	ll q; cin>>q;
	while(q--)
	{
	   ll n,ans=0; cin>>n;
	   set<ll>s;
	   vector<ll>v;
	   for(ll i=0;i<n;i++)
	   {
	       ll x; cin>>x;
	       if((x&1)==0) s.insert(x);
	   }

	   //sort(v.begin(),v.end(),greater<ll>());
	   while(1)
	   {
	       ll ma=INT_MIN,p,i=0;
	       set<ll>::iterator it=s.begin();
	   for(it=s.begin();it!=s.end();it++)
       {
           ll x; x=*it;
           if(x>ma) {p=i;ma=x;}
           i++;
       }
	   //cout<<v[0]<<endl;
	   if(ma==INT_MIN) break;
	   ma/=2;
	   it=s.begin();
	   while(p--)
       {
           it++;
       }
	        s.erase(it);
	       if(ma%2==0) s.insert(ma);
	   ans++;
	   }
	    cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
	ll q; cin>>q;
	while(q--)
	{
	   ll n,ans=0; cin>>n;
	   vector<ll>v;
	   for(ll i=0;i<n;i++)
	   {
	       ll x; cin>>x;
	       if(x%2==0) v.push_back(x);
	   }
	   //sort(v.begin(),v.end(),greater<ll>());
	   while(1)
	   {
	       ll ma=INT_MIN;
	   for(ll i=0;i<v.size();i++)
	   {
	       if(v[i]%2==0 && ma<v[i]) { ma=v[i];}
	   }
	   //cout<<v[0]<<endl;
	   if(ma==INT_MIN) break;
	   for(ll i=0;i<v.size();i++)
	   {
	       if(ma==v[i]) v[i]/=2;
	   }
	   ans++;
	   }
	    cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl "\n";
void nitesh()
{
   ll n; cin>>n;
   set<ll>s;
   for(ll i=0;i<n;i++)
   {
       ll x; cin>>x;
       s.insert(x);
   }
   ll ans=0;
   while(!s.empty())
   {
       int p=*s.rbegin();
      s.erase(p);
      if(p%2==0)
      {
          s.insert(p/2);
          ans++;
      }
   }
   cout<<ans<<endl;
}
int main()
{
  ll q;  cin>>q;
  while(q--)
  {
        nitesh();
  }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
	ll q; cin>>q;
	while(q--)
	{
	   ll n,ans=0; cin>>n;
	   vector<ll>v;
	   for(ll i=0;i<n;i++)
	   {
	       ll x; cin>>x;
	       if(x%2==0) v.push_back(x);
	   }
	   sort(v.begin(),v.end(),greater<ll>());
	   while(1)
	   {
	       ll ma=INT_MIN,p;
	   for(ll i=0;i<v.size();i++)
	   {
	       if(v[i]%2==0 && ma<v[i]) {p=i; ma=v[i];}
	   }
	   //cout<<v[0]<<endl;
	   if(ma==INT_MIN) break;
	   for(ll i=0;i<v.size();i++)
	   {
	       if(ma==v[i]) v[i]/=2;
	   }
	   ans++;
	   }
	    cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int re[200009], L = 0;
int v[200009];
int a[200009];
void fun(int x, int cnt) {
	int t;
	bool ok = false;
	for (int i = 0; i < L; i++) {
		if (x == re[i]) {
			t = i;
			ok = true;
		}
	}
	if (ok) {
		if (cnt > v[t]) {
			v[t] = cnt;
		}
	}
	else {
		re[L] = x;
		v[L++] = cnt;
	}
}
int main() {
	int T;
	cin >> T;
	while (T--) {
		int n;
		long long sum = 0;
		L = 0;
		cin >> n;
		for (int i = 0; i < n; i++) {
			scanf("%d", &a[i]);
		}
		for (int i = 0; i < n; i++) {
			if (a[i] % 2 == 0) {
				int t = a[i], cnt = 0;
				while (t % 2 == 0) {
					t = t / 2;
					cnt++;
				}
				fun(t, cnt);
			}
		}
		for (int i = 0; i < L; i++) {
			sum += v[i];
		}
		cout << sum << endl;
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
int book[200009] = { 0 };
int main() {
	int T;
	cin >> T;
	while (T--) {
		memset(book, 0, sizeof(book));
		int n, x;
		scanf("%d", &n);
		int max = -1;
		for (int i = 0; i < n; i++) {
			scanf("%d", &x);
			if (x > max) {
				max = x;
			}
			book[x] = 1;
		}
		int cnt = 0;
		for (int i = max; i > 1; i--) {
			if (book[i] == 1 && i % 2 == 0) {
				book[i / 2] = 1;
				cnt++;
			}
		}
		printf("%d\n", cnt);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
int a[200009];
int main() {
	int T;
	cin >> T;
	while (T--) {
		int n;
		long long sum = 0;
		map<int, int> v;
		map<int, int>::iterator iter;
		cin >> n;
		for (int i = 0; i < n; i++) {
			scanf("%d", &a[i]);
		}

		for (int i = 0; i < n; i++) {
			if (a[i] % 2 == 0) {
				int t = a[i], cnt = 0;
				while (t % 2 == 0) {
					t = t / 2;
					cnt++;
				}
				iter = v.find(t);
				if (iter != v.end()) {
					if (cnt > v[t]) {
						v[t] = cnt;
					}
				}
				else {
					v[t] = cnt;
				}
			}
		}
		for (iter = v.begin(); iter != v.end(); iter++) {
			sum += iter->second;
		}
		cout << sum << endl;
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int re[200009], L = 0;
int a[200009];
bool find(int x) {
	for (int i = 0; i < L; i++) {
		if (re[i] == x) {
			return 1;
		}
	}
	return 0;
}
int main() {
	int T;
	cin >> T;
	while (T--) {
		int n;
		long long sum = 0;
		L = 0;
		cin >> n;
		for (int i = 0; i < n; i++) {
			scanf_s("%d", &a[i]);
		}
		sort(a, a + n, greater<int>());
		for (int i = 0; i < n; i++) {
			if (a[i] % 2 == 0) {
				int t = a[i], cnt = 0;
				while (t % 2 == 0) {
					t = t / 2;
					cnt++;
				}
				if (find(t)) {
					;
				}
				else {
					re[L++] = t;
					sum += cnt;
				}
			}
		}
		cout << sum << endl;
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int re[200009], L = 0;
int a[200009];
bool find(int x) {
	for (int i = 0; i < L; i++) {
		if (re[i] == x) {
			return 1;
		}
	}
	return 0;
}
int main() {
	int T;
	cin >> T;
	while (T--) {
		int n;
		long long sum = 0;
		L = 0;
		cin >> n;
		for (int i = 0; i < n; i++) {
			scanf("%d", &a[i]);
		}
		sort(a, a + n, greater<int>());
		for (int i = 0; i < n; i++) {
			if (a[i] % 2 == 0) {
				int t = a[i], cnt = 0;
				while (t % 2 == 0) {
					t = t / 2;
					cnt++;
				}
				if (find(t)) {
					;
				}
				else {
					re[L++] = t;
					sum += cnt;
				}
			}
		}
		cout << sum << endl;
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
int book[200009] = { 0 };
int main() {
	int T;
	cin >> T;
	while (T--) {
		memset(book, 0, sizeof(book));
		int n, x;
		scanf_s("%d", &n);
		int max = -1;
		for (int i = 0; i < n; i++) {
			scanf_s("%d", &x);
			if (x > max) {
				max = x;
			}
			book[x] = 1;
		}
		int cnt = 0;
		for (int i = max; i > 1; i--) {
			if (book[i] == 1 && i % 2 == 0) {
				book[i / 2] = 1;
				cnt++;
			}
		}
		printf("%d\n", cnt);
	}
	return 0;
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n;
    cin>>n;
    while(n--)
    {
      long long a1;
      cin>>a;
      long long arr[a1];
      long long ans=0;
      set<long long>m1;
      for(long long i=0;i<a1;i++)
      {
        cin>>arr[i];
           m1.insert(arr[i]);
      }
      long long result=0;
      while(!m1.empty())
      {
          long long a=*m1.rbegin();
          m1.erase(a);
          if(a%2==0)
          {
              result++;
              m1.insert(a/2);
          }

      }
      cout<<result<<endl;
    }
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n;
    cin>>n;
    while(n--)
    {
      long long a1;
      cin>>a1;
      long long arr[a1];
      long long ans=0;
      set<long long>m1;
      for(long long i=0;i<a1;i++)
      {
        cin>>arr[i];
           m1.insert(arr[i]);
      }
      long long result=0;
      while(!m1.empty())
      {
          long long a=*m1.rbegin();
          m1.erase(a);
          if(a%2==0)
          {
              result++;
              m1.insert(a/2);
          }

      }
      cout<<result<<endl;
    }
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n;
    cin>>n;
    while(n--)
    {
      long long a;
      cin>>a;
      long long arr[n];
      long long ans=0;
      set<long long>m1;
      for(long long i=0;i<a;i++)
      {
        cin>>arr[i];
           m1.insert(arr[i]);
      }
      long long result=0;
      while(!m1.empty())
      {
          long long a=*m1.rbegin();
          m1.erase(a);
          if(a%2==0)
          {
              result++;
              m1.insert(a/2);
          }

      }
      cout<<result<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
short sum[10000000];
int main(){
	int T;
	cin>>T;
	while(T--){
		memset(sum,0,sizeof(sum));
		ll n;
		ll nas=0;
		scanf("%lld",&n);
		int k=0;
		for(int i=1;i<=n;i++){
			int p;
			short pp=0;
			scanf("%d",&p);
			while(p%2==0){
				p>>=1;
				pp++;
			}
			sum[p]=max(sum[p],pp);
			k=max(p,k);
		}
		for(int i=1;i<=k;i+=2){
			nas+=sum[i];
		}
		printf("%lld\n",nas);
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
short sum[100000000];
int main(){
	int T;
	cin>>T;
	while(T--){
		ll n;
		ll nas=0;
		scanf("%lld",&n);
		int k=0;
		for(int i=1;i<=n;i++){
			int p;
			short pp=0;
			scanf("%d",&p);
			while(p%2==0){
				p>>=1;
				pp++;
			}
			if(p>100000000){
				nas+=pp;
				continue;
			}
			sum[p]=max(sum[p],pp);
			k=max(p,k);
		}
		for(int i=1;i<=k;i+=2){
			nas+=sum[i];
			sum[i]=0;
		}
		printf("%lld\n",nas);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
short sum[10000000];
int main(){
	int T;
	cin>>T;
	while(T--){
		ll n;
		ll nas=0;
		scanf("%lld",&n);
		int k=0;
		for(int i=1;i<=n;i++){
			int p;
			short pp=0;
			scanf("%d",&p);
			while(p%2==0){
				p>>=1;
				pp++;
			}
			sum[p]=max(sum[p],pp);
			k=max(p,k);
		}
		for(int i=1;i<=k;i+=2){
			nas+=sum[i];
			sum[i]=0;
		}
		printf("%lld\n",nas);
	}
	return 0;
}


//https://leetcode.com/problems/range-sum-query-2d-mutable/
//41  24
#include <bits/stdc++.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
using namespace std;
long long MOD = 1e9+7;
long long M = 550;
long long m, t, k, a, b;
long long t0, t1;
long long n;
int main() {
    vector<long long> ans;
    vector<pair<long long, long long>> a,b;
    scanf("%lld %lld", &k, &n);

    long long tt = 0;
    for (int i = 1; i <= k; i++) {
        scanf("%lld %lld", &t0, &t1);;
        if (t0 == 1) {
            a.push_back({t1, i});
        } else {
            b.push_back({t1, i});
        }
        tt += t1;
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    long long ap = a.size() - 1;
    long long bp = b.size() - 1;
    long long total = 0;
    if (n % 2 == 1 && ap >= 0) {
        ans.push_back(a[ap].second);
        total += a[ap].first;
        ap --;
        n --;
    }

    long long cnt = 0;
    while (n > 0 && (ap >= 0 || bp >= 0)) {
        cnt ++;
        if (cnt > 200000) {
            printf("[%lld]", n);
            break;
        }
        long long ao = 0;
        long long bo = 0;
        if (ap >= 0) {
            ao += a[ap].first;
        }
        if (ap - 1 >= 0 && n >= 2) {
            ao += a[ap  -1].first;
        }
        if (bo >= 0 && n >= 2) {
            bo += b[bp].first;
        }
        if (ao == 0 && bo == 0) break;

        if (tt == 4016414) {
            printf("%d %d|}", ao, bo);
        }
        if (ao < bo) {
            if (bp >= 0) {
                ans.push_back(b[bp].second);
                total += b[bp].first;
                bp --;
                n-=2;
            }
        } else {
             for (int i = 0; i < 2; i++) {
                if (ap >= 0) {
                    ans.push_back(a[ap].second);
                    total += a[ap].first;
                    ap --;
                    n --;
                }
            }
        }
    }
    //if (4016387 == total) printf("%d\n", (int)ans.size());
    printf("%lld\n", total);
    for (int i = 0; i < ans.size(); i++) {
        printf("%lld ", ans[i]);
    }
}


//https://leetcode.com/problems/range-sum-query-2d-mutable/
//41  24
#include <bits/stdc++.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
using namespace std;
long long MOD = 1e9+7;
long long M = 550;
long long m, t, k, a, b;
long long t0, t1;
long long n;
int main() {
    vector<long long> ans;
    vector<pair<long long, long long>> a,b;
    scanf("%lld %lld", &k, &n);

    long long tt = 0;
    for (int i = 1; i <= k; i++) {
        scanf("%lld %lld", &t0, &t1);;
        if (t0 == 1) {
            a.push_back({t1, i});
        } else {
            b.push_back({t1, i});
        }
        tt += t1;
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    long long ap = a.size() - 1;
    long long bp = b.size() - 1;
    long long total = 0;
    if (n % 2 == 1 && ap >= 0) {
        ans.push_back(a[ap].second);
        total += a[ap].first;
        ap --;
        n --;
    }

    long long cnt = 0;
    while (n > 0 && (ap >= 0 || bp >= 0)) {
        cnt ++;
        if (cnt > 200000) {
            printf("[%lld]", n);
            break;
        }
        long long ao = 0;
        long long bo = 0;
        if (ap >= 0) {
            ao += a[ap].first;
        }
        if (ap - 1 >= 0 && n >= 2) {
            ao += a[ap  -1].first;
        }
        if (bo >= 0 && n >= 2) {
            bo += b[bp].first;
        }
        if (ao == 0 && bo == 0) break;

        if (tt == 4016414) {
            printf("%d %d %d|", ao, bo, n);
        }
        if (ao < bo) {
            if (bp >= 0) {
                ans.push_back(b[bp].second);
                total += b[bp].first;
                bp --;
                n-=2;
            }
        } else {
             for (int i = 0; i < 2; i++) {
                if (ap >= 0) {
                    ans.push_back(a[ap].second);
                    total += a[ap].first;
                    ap --;
                    n --;
                }
            }
        }
    }
    //if (4016387 == total) printf("%d\n", (int)ans.size());
    printf("%lld\n", total);
    for (int i = 0; i < ans.size(); i++) {
        printf("%lld ", ans[i]);
    }
}


//https://leetcode.com/problems/range-sum-query-2d-mutable/
//41  24
#include <bits/stdc++.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
using namespace std;
long long MOD = 1e9+7;
long long M = 550;
int n, m, t, k, a, b;
int t0, t1;

int main() {
    vector<int> ans;
    vector<pair<int, int>> a,b;
    scanf("%d %d", &k, &n);

    for (int i = 1; i <= k; i++) {
        scanf("%d %d", &t0, &t1);;
        if (t0 == 1) {
            a.push_back({t1, i});
        } else {
            b.push_back({t1, i});
        }
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    int ap = a.size() - 1;
    int bp = b.size() - 1;
    long long total = 0;
    if (n % 2 == 1 && ap >= 0) {
        ans.push_back(a[ap].second);
        total += a[ap].first;
        ap --;
        n --;
    }

    while (n > 0 && (ap >= 0 || bp >= 0)) {
        int ao = 0;
        int bo = 0;
        if (ap >= 0) {
            ao += a[ap].first;
        }
        if (ap - 1 >= 0 && n >= 2) {
            ao += a[ap  -1].first;
        }
        if (bo >= 0 && n >= 2) {
            bo += b[bp].first;
        }
        if (ao == 0 && bo == 0) break;
        if (ao < bo) {
            if (bp >= 0) {
                ans.push_back(b[bp].second);
                total += b[bp].first;
                bp --;
                n-=2;
            }
        } else {
             for (int i = 0; i < 2; i++) {
                if (ap >= 0) {
                    ans.push_back(a[ap].second);
                    total += a[ap].first;
                    ap --;
                    n --;
                }
            }
        }
    }
    printf("%lld\n", total);
    for (int i = 0; i < ans.size(); i++) {
        printf("%d ", ans[i]);
    }
}


//https://leetcode.com/problems/range-sum-query-2d-mutable/
//41  24
#include <bits/stdc++.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
using namespace std;
long long MOD = 1e9+7;
long long M = 550;
long long m, t, k, a, b;
long long t0, t1;
long long n;
int main() {
    vector<long long> ans;
    vector<pair<long long, long long>> a,b;
    scanf("%lld %lld", &k, &n);

    long long tt = 0;
    for (int i = 1; i <= k; i++) {
        scanf("%lld %lld", &t0, &t1);;
        if (t0 == 1) {
            a.push_back({t1, i});
        } else {
            b.push_back({t1, i});
        }
        tt += t1;
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    long long ap = (int)a.size() - 1;
    long long bp = (int)b.size() - 1;
    long long total = 0;
    if (n % 2 == 1 && ap >= 0) {
        ans.push_back(a[ap].second);
        total += a[ap].first;
        ap --;
        n --;
    }

    long long cnt = 0;
    while (n > 0 && (ap >= 0 || bp >= 0)) {
        cnt ++;
        if (cnt > 200000) {
            printf("[%lld]", n);
            break;
        }
        long long ao = 0;
        long long bo = 0;
        if (ap >= 0) {
            ao += a[ap].first;
        }
        if (ap - 1 >= 0 && n >= 2) {
            ao += a[ap  -1].first;
        }
        if (bo >= 0 && n >= 2) {
            bo += b[bp].first;
        }
        if (ao == 0 && bo == 0) break;

        if (tt == 4016414) {
            printf("%d %d %d, %d %d|", ao, bo, n, ap, bp);
        }
        if (ao < bo) {
            if (bp >= 0) {
                ans.push_back(b[bp].second);
                total += b[bp].first;
                bp --;
                n-=2;
            }
        } else {
             for (int i = 0; i < 2; i++) {
                if (ap >= 0) {
                    ans.push_back(a[ap].second);
                    total += a[ap].first;
                    ap --;
                    n --;
                }
            }
        }
    }
    //if (4016387 == total) printf("%d\n", (int)ans.size());
    printf("%lld\n", total);
    for (int i = 0; i < ans.size(); i++) {
        printf("%lld ", ans[i]);
    }
}


//https://leetcode.com/problems/range-sum-query-2d-mutable/
//41  24
#include <bits/stdc++.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
using namespace std;
long long MOD = 1e9+7;
long long M = 550;
int m, t, k, a, b;
int t0, t1;
long long n;
int main() {
    vector<int> ans;
    vector<pair<int, int>> a,b;
    scanf("%d %lld", &k, &n);

    for (int i = 1; i <= k; i++) {
        scanf("%d %d", &t0, &t1);;
        if (t0 == 1) {
            a.push_back({t1, i});
        } else {
            b.push_back({t1, i});
        }
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    int ap = a.size() - 1;
    int bp = b.size() - 1;
    long long total = 0;
    if (n % 2 == 1 && ap >= 0) {
        ans.push_back(a[ap].second);
        total += a[ap].first;
        ap --;
        n --;
    }

    int cnt = 0;
    while (n > 0 && (ap >= 0 || bp >= 0)) {
        cnt ++;
        if (cnt > 200000) {
            printf("[%lld]", n);
            break;
        }
        int ao = 0;
        int bo = 0;
        if (ap >= 0) {
            ao += a[ap].first;
        }
        if (ap - 1 >= 0 && n >= 2) {
            ao += a[ap  -1].first;
        }
        if (bo >= 0 && n >= 2) {
            bo += b[bp].first;
        }
        if (ao == 0 && bo == 0) break;
        if (ao < bo) {
            if (bp >= 0) {
                ans.push_back(b[bp].second);
                total += b[bp].first;
                bp --;
                n-=2;
            }
        } else {
             for (int i = 0; i < 2; i++) {
                if (ap >= 0) {
                    ans.push_back(a[ap].second);
                    total += a[ap].first;
                    ap --;
                    n --;
                }
            }
        }
    }
    if (4016387 == total) printf("%d\n", ans.size());
    printf("%lld\n", total);
    for (int i = 0; i < ans.size(); i++) {
        printf("%d ", ans[i]);
    }
}


//https://leetcode.com/problems/range-sum-query-2d-mutable/
//41  24
#include <bits/stdc++.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
using namespace std;
long long MOD = 1e9+7;
long long M = 550;
int m, t, k, a, b;
int t0, t1;
long long n;
int main() {
    vector<int> ans;
    vector<pair<int, int>> a,b;
    scanf("%d %lld", &k, &n);

    long long tt = 0;
    for (int i = 1; i <= k; i++) {
        scanf("%d %d", &t0, &t1);;
        if (t0 == 1) {
            a.push_back({t1, i});
        } else {
            b.push_back({t1, i});
        }
        tt += t1;
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    int ap = a.size() - 1;
    int bp = b.size() - 1;
    long long total = 0;
    if (n % 2 == 1 && ap >= 0) {
        ans.push_back(a[ap].second);
        total += a[ap].first;
        ap --;
        n --;
    }

    int cnt = 0;
    while (n > 0 && (ap >= 0 || bp >= 0)) {
        cnt ++;
        if (cnt > 200000) {
            printf("[%lld]", n);
            break;
        }
        int ao = 0;
        int bo = 0;
        if (ap >= 0) {
            ao += a[ap].first;
        }
        if (ap - 1 >= 0 && n >= 2) {
            ao += a[ap  -1].first;
        }
        if (bo >= 0 && n >= 2) {
            bo += b[bp].first;
        }
        if (ao == 0 && bo == 0) break;
        if (tt == 4016414 && cnt == 100000) {
            printf("{%d %d}", ao, bo);
        }
        if (ao < bo) {
            if (bp >= 0) {
                ans.push_back(b[bp].second);
                total += b[bp].first;
                bp --;
                n-=2;
            }
        } else {
             for (int i = 0; i < 2; i++) {
                if (ap >= 0) {
                    ans.push_back(a[ap].second);
                    total += a[ap].first;
                    ap --;
                    n --;
                }
            }
        }
    }
    if (4016387 == total) printf("%d\n", (int)ans.size());
    printf("%lld\n", total);
    for (int i = 0; i < ans.size(); i++) {
        printf("%d ", ans[i]);
    }
}


//https://leetcode.com/problems/range-sum-query-2d-mutable/
//41  24
#include <bits/stdc++.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
using namespace std;
long long MOD = 1e9+7;
long long M = 550;
long long m, t, k, a, b;
long long t0, t1;
long long n;
int main() {
    vector<long long> ans;
    vector<pair<long long, long long>> a,b;
    scanf("%lld %lld", &k, &n);

    long long tt = 0;
    for (int i = 1; i <= k; i++) {
        scanf("%lld %lld", &t0, &t1);;
        if (t0 == 1) {
            a.push_back({t1, i});
        } else {
            b.push_back({t1, i});
        }
        tt += t1;
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    long long ap = (int)a.size() - 1;
    long long bp = (int)b.size() - 1;
    long long total = 0;
    if (n % 2 == 1 && ap >= 0) {
        ans.push_back(a[ap].second);
        total += a[ap].first;
        ap --;
        n --;
    }

    long long cnt = 0;
    while (n > 0 && (ap >= 0 || bp >= 0)) {
        cnt ++;
        long long ao = 0;
        long long bo = 0;
        if (ap >= 0) {
            ao += a[ap].first;
        }
        if (ap - 1 >= 0 && n >= 2) {
            ao += a[ap  -1].first;
        }
        if (bo >= 0 && n >= 2) {
            bo += b[bp].first;
        }
        if (ao == 0 && bo == 0) break;

        if (ao < bo) {
            if (bp >= 0) {
                ans.push_back(b[bp].second);
                total += b[bp].first;
                bp --;
                n-=2;
            }
        } else {
             for (int i = 0; i < 2; i++) {
                if (ap >= 0) {
                    ans.push_back(a[ap].second);
                    total += a[ap].first;
                    ap --;
                    n --;
                }
            }
        }
    }
    //if (4016387 == total) printf("%d\n", (int)ans.size());
    printf("%lld\n", total);
    for (int i = 0; i < ans.size(); i++) {
        printf("%lld ", ans[i]);
    }
}


//https://leetcode.com/problems/range-sum-query-2d-mutable/
//41  24
#include <bits/stdc++.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
using namespace std;
long long MOD = 1e9+7;
long long M = 550;
int m, t, k, a, b;
int t0, t1;
long long n;
int main() {
    vector<int> ans;
    vector<pair<int, int>> a,b;
    scanf("%d %lld", &k, &n);

    for (int i = 1; i <= k; i++) {
        scanf("%d %d", &t0, &t1);;
        if (t0 == 1) {
            a.push_back({t1, i});
        } else {
            b.push_back({t1, i});
        }
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    int ap = a.size() - 1;
    int bp = b.size() - 1;
    long long total = 0;
    if (n % 2 == 1 && ap >= 0) {
        ans.push_back(a[ap].second);
        total += a[ap].first;
        ap --;
        n --;
    }

    int cnt = 0;
    while (n > 0 && (ap >= 0 || bp >= 0)) {
        cnt ++;
        if (cnt > 200000) {
            printf("[%lld]", n);
            break;
        }
        int ao = 0;
        int bo = 0;
        if (ap >= 0) {
            ao += a[ap].first;
        }
        if (ap - 1 >= 0 && n >= 2) {
            ao += a[ap  -1].first;
        }
        if (bo >= 0 && n >= 2) {
            bo += b[bp].first;
        }
        if (ao == 0 && bo == 0) break;
        if (ao < bo) {
            if (bp >= 0) {
                ans.push_back(b[bp].second);
                total += b[bp].first;
                bp --;
                n-=2;
            }
        } else {
             for (int i = 0; i < 2; i++) {
                if (ap >= 0) {
                    ans.push_back(a[ap].second);
                    total += a[ap].first;
                    ap --;
                    n --;
                }
            }
        }
    }
    if (4016387 == total) printf("%d\n", (int)ans.size());
    printf("%lld\n", total);
    for (int i = 0; i < ans.size(); i++) {
        printf("%d ", ans[i]);
    }
}


//https://leetcode.com/problems/range-sum-query-2d-mutable/
//41  24
#include <bits/stdc++.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
using namespace std;
long long MOD = 1e9+7;
long long M = 550;
int n, m, t, k, a, b;
int t0, t1;

int main() {
    vector<int> ans;
    vector<pair<int, int>> a,b;
    scanf("%d %d", &k, &n);

    for (int i = 1; i <= k; i++) {
        scanf("%d %d", &t0, &t1);;
        if (t0 == 1) {
            a.push_back({t1, i});
        } else {
            b.push_back({t1, i});
        }
    }
    if (2* b.size() + a.size() <= n) {
        long long  total = 0;
        for (auto i: a) {
            total += i.first;
        }
        for (auto i: b) {
            total += i.first;
        }
        printf("%lld\n", total);
        for (int i = 1; i <= k; i++) {
            printf("%d ", i);
        }
        return 0;
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    int ap = a.size() - 1;
    int bp = b.size() - 1;
    long long total = 0;
    if (n % 2 == 1 && ap >= 0) {
        ans.push_back(a[ap].second);
        total += a[ap].first;
        ap --;
        n --;
    }

    while (n > 0 && (ap >= 0 || bp >= 0)) {
        int ao = 0;
        int bo = 0;
        if (ap >= 0) {
            ao += a[ap].first;
        }
        if (ap - 1 >= 0 && n >= 2) {
            ao += a[ap  -1].first;
        }
        if (bo >= 0 && n >= 2) {
            bo += b[bp].first;
        }
        if (ao == 0 && bo == 0) break;
        if (ao < bo) {
            if (bp >= 0) {
                ans.push_back(b[bp].second);
                total += b[bp].first;
                bp --;
                n-=2;
            }
        } else {
             for (int i = 0; i < 2; i++) {
                if (ap >= 0) {
                    ans.push_back(a[ap].second);
                    total += a[ap].first;
                    ap --;
                    n --;
                }
            }
        }
    }
    printf("%lld\n", total);
    for (int i = 0; i < ans.size(); i++) {
        printf("%d ", ans[i]);
    }
}


//https://leetcode.com/problems/range-sum-query-2d-mutable/
//41  24
#include <bits/stdc++.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
using namespace std;
long long MOD = 1e9+7;
long long M = 550;
long long m, t, k, a, b;
long long t0, t1;
long long n;
int main() {
    vector<long long> ans;
    vector<pair<long long, long long>> a,b;
    scanf("%lld %lld", &k, &n);

    long long tt = 0;
    for (int i = 1; i <= k; i++) {
        scanf("%lld %lld", &t0, &t1);;
        if (t0 == 1) {
            a.push_back({t1, i});
        } else {
            b.push_back({t1, i});
        }
        tt += t1;
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    long long ap = - 1 + (int)a.size() ;
    long long bp = -1 +(int)b.size();
    long long total = 0;
    if (n % 2 == 1 && ap >= 0) {
        ans.push_back(a[ap].second);
        total += a[ap].first;
        ap --;
        n --;
    }

    long long cnt = 0;
    long long lastN = 0;
    while (n > 0 && (ap >= 0 || bp >= 0)) {
        cnt ++;
        if (cnt > 200000) {
            printf("[%lld]", n);
            break;
        }
        long long ao = 0;
        long long bo = 0;
        if (ap >= 0) {
            ao += a[ap].first;
        }
        if (ap - 1 >= 0 && n >= 2) {
            ao += a[ap  -1].first;
        }
        if (bo >= 0 && n >= 2) {
            bo += b[bp].first;
        }
        if (ao == 0 && bo == 0) break;

        if (ao < bo) {
            if (bp >= 0) {
                ans.push_back(b[bp].second);
                total += b[bp].first;
                bp --;
                n-=2;
            }
        } else {
             for (int i = 0; i < 2; i++) {
                if (ap >= 0) {
                    ans.push_back(a[ap].second);
                    total += a[ap].first;
                    ap --;
                    n --;
                }
            }
        }
        if (tt == 4016414) {
            if (lastN == n)
             printf("%lld %lld %lld, %lld %lld|", ao, bo, n, ap, bp);
        }
        lastN = n;
    }
    //if (4016387 == total) printf("%d\n", (int)ans.size());
    printf("%lld\n", total);
    for (int i = 0; i < ans.size(); i++) {
        printf("%lld ", ans[i]);
    }
}


//https://leetcode.com/problems/range-sum-query-2d-mutable/
//41  24
#include <bits/stdc++.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
using namespace std;
long long MOD = 1e9+7;
long long M = 550;
long long m, t, k, a, b;
long long t0, t1;
long long n;
int main() {
    vector<long long> ans;
    vector<pair<long long, long long>> a,b;
    scanf("%lld %lld", &k, &n);

    long long tt = 0;
    for (int i = 1; i <= k; i++) {
        scanf("%lld %lld", &t0, &t1);;
        if (t0 == 1) {
            a.push_back({t1, i});
        } else {
            b.push_back({t1, i});
        }
        tt += t1;
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    long long ap = - 1 + (int)a.size() ;
    long long bp = -1 +(int)b.size();
    long long total = 0;
    if (n % 2 == 1 && ap >= 0) {
        ans.push_back(a[ap].second);
        total += a[ap].first;
        ap --;
        n --;
    }

    long long cnt = 0;
    long long lastN = 0;
    while (n > 0 && (ap >= 0 || bp >= 0)) {
        cnt ++;
        if (cnt > 200000) {
            printf("[%lld]", n);
            break;
        }
        long long ao = 0;
        long long bo = 0;
        if (ap >= 0) {
            ao += a[ap].first;
        }
        if (ap - 1 >= 0 && n >= 2) {
            ao += a[ap  -1].first;
        }
        if (bo >= 0 && n >= 2) {
            bo += b[bp].first;
        }
        if (ao == 0 && bo == 0) break;

        if (tt == 4016414) {
            if (lastN == n)
             printf("%lld %lld %lld, %lld %lld|", ao, bo, n, ap, bp);
        }
        if (ao < bo) {
            if (bp >= 0) {
                ans.push_back(b[bp].second);
                total += b[bp].first;
                bp --;
                n-=2;
            }
        } else {
             for (int i = 0; i < 2; i++) {
                if (ap >= 0) {
                    ans.push_back(a[ap].second);
                    total += a[ap].first;
                    ap --;
                    n --;
                }
            }
        }
        lastN = n;
    }
    //if (4016387 == total) printf("%d\n", (int)ans.size());
    printf("%lld\n", total);
    for (int i = 0; i < ans.size(); i++) {
        printf("%lld ", ans[i]);
    }
}


//https://leetcode.com/problems/range-sum-query-2d-mutable/
//41  24
#include <bits/stdc++.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
using namespace std;
long long MOD = 1e9+7;
long long M = 550;
int n, m, t, k, a, b;
int t0, t1;

int main() {
    vector<int> ans;
    vector<pair<int, int>> a,b;
    scanf("%d %d", &k, &n);

    for (int i = 1; i <= k; i++) {
        scanf("%d %d", &t0, &t1);;
        if (t0 == 1) {
            a.push_back({t1, i});
        } else {
            b.push_back({t1, i});
        }
    }
    if (2* b.size() + a.size() <= n) {
        long long  total = 0;
        for (auto i: a) {
            total += i.first;
        }
        for (auto i: b) {
            total += i.first;
        }
        printf("%lld\n", total);
        for (int i = 1; i <= k; i++) {
            printf("%d ", i);
        }
        return 0;
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    int ap = a.size() - 1;
    int bp = b.size() - 1;
    long long total = 0;
    if (n % 2 == 1 && ap >= 0) {
        ans.push_back(a[ap].second);
        total += a[ap].first;
        ap --;
        n --;
    }

    while (n > 0 && (ap >= 0 || bp >= 0)) {
        int ao = 0;
        int bo = 0;
        if (ap >= 0) {
            ao += a[ap].first;
        }
        if (ap - 1 >= 0 && n >= 2) {
            ao += a[ap  -1].first;
        }
        if (bo >= 0 && n >= 2) {
            bo += b[bp].first;
        }
        if (ao > bo) {
            for (int i = 0; i < 2; i++) {
                if (ap >= 0) {
                    ans.push_back(a[ap].second);
                    total += a[ap].first;
                    ap --;
                    n --;
                }
            }
        } else {
            if (bp >= 0) {
                ans.push_back(b[bp].second);
                total += b[bp].first;
                bp --;
                n-=2;
            }
        }
    }
    printf("%lld\n", total);
    for (int i = 0; i < ans.size(); i++) {
        printf("%d ", ans[i]);
    }
}


//https://leetcode.com/problems/range-sum-query-2d-mutable/
//41  24
#include <bits/stdc++.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
using namespace std;
long long MOD = 1e9+7;
long long M = 550;
int n, m, t, k, a, b;
int t0, t1;

int main() {
    vector<int> ans;
    vector<pair<int, int>> a,b;
    scanf("%d %d", &k, &n);

    for (int i = 1; i <= k; i++) {
        scanf("%d %d", &t0, &t1);;
        if (t0 == 1) {
            a.push_back({t1, i});
        } else {
            b.push_back({t1, i});
        }
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    int ap = a.size() - 1;
    int bp = b.size() - 1;
    long long total = 0;
    if (n % 2 == 1 && ap >= 0) {
        ans.push_back(a[ap].second);
        total += a[ap].first;
        ap --;
        n --;
    }

    while (n > 0 && (ap >= 0 || bp >= 0)) {
        int ao = 0;
        int bo = 0;
        if (ap >= 0) {
            ao += a[ap].first;
        }
        if (ap - 1 >= 0 && n >= 2) {
            ao += a[ap  -1].first;
        }
        if (bo >= 0 && n >= 2) {
            bo += b[bp].first;
        }
        if (ao > bo) {
            for (int i = 0; i < 2; i++) {
                if (ap >= 0) {
                    ans.push_back(a[ap].second);
                    total += a[ap].first;
                    ap --;
                    n --;
                }
            }
        } else {
            if (bp >= 0) {
                ans.push_back(b[bp].second);
                total += b[bp].first;
                bp --;
                n-=2;
            }
        }
    }
    printf("%d\n", total);
    for (int i = 0; i < ans.size(); i++) {
        printf("%d ", ans[i]);
    }
}


//https://leetcode.com/problems/range-sum-query-2d-mutable/
//41  24
#include <bits/stdc++.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
using namespace std;
long long MOD = 1e9+7;
long long M = 550;
int n, m, t, k, a, b;
int t0, t1;

int main() {
    vector<int> ans;
    vector<pair<int, int>> a,b;
    scanf("%d %d", &k, &n);

    for (int i = 1; i <= k; i++) {
        scanf("%d %d", &t0, &t1);;
        if (t0 == 1) {
            a.push_back({t1, i});
        } else {
            b.push_back({t1, i});
        }
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    int ap = a.size() - 1;
    int bp = b.size() - 1;
    long long total = 0;
    if (n % 2 == 1 && ap >= 0) {
        ans.push_back(a[ap].second);
        total += a[ap].first;
        ap --;
        n --;
    }

    int cnt = 0;
    while (n > 0 && (ap >= 0 || bp >= 0)) {
        cnt ++;
        if (cnt > 200000) {
            break;
        }
        int ao = 0;
        int bo = 0;
        if (ap >= 0) {
            ao += a[ap].first;
        }
        if (ap - 1 >= 0 && n >= 2) {
            ao += a[ap  -1].first;
        }
        if (bo >= 0 && n >= 2) {
            bo += b[bp].first;
        }
        if (ao == 0 && bo == 0) break;
        if (ao < bo) {
            if (bp >= 0) {
                ans.push_back(b[bp].second);
                total += b[bp].first;
                bp --;
                n-=2;
            }
        } else {
             for (int i = 0; i < 2; i++) {
                if (ap >= 0) {
                    ans.push_back(a[ap].second);
                    total += a[ap].first;
                    ap --;
                    n --;
                }
            }
        }
    }
    printf("%lld\n", total);
    for (int i = 0; i < ans.size(); i++) {
        printf("%d ", ans[i]);
    }
}


//https://leetcode.com/problems/range-sum-query-2d-mutable/
//41  24
#include <bits/stdc++.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
using namespace std;
long long MOD = 1e9+7;
long long M = 550;
int n, m, t, k, a, b;
long long t0, t1;

int main() {
    vector<long long> ans;
    vector<pair<long long, int>> a,b;
    scanf("%d %d", &k, &n);

    for (int i = 1; i <= k; i++) {
        scanf("%lld %lld", &t0, &t1);;
        if (t0 == 1) {
            a.push_back({t1, i});
        } else {
            b.push_back({t1, i});
        }
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    int ap = a.size() - 1;
    int bp = b.size() - 1;
    long long total = 0;
    if (n % 2 == 1 && ap >= 0) {
        ans.push_back(a[ap].second);
        total += a[ap].first;
        ap --;
        n --;
    }

    while (n > 0 && (ap >= 0 || bp >= 0)) {
        long long ao = 0;
        long long bo = 0;
        if (ap >= 0) {
            ao += a[ap].first;
        }
        if (ap - 1 >= 0 && n >= 2) {
            ao += a[ap  -1].first;
        }
        if (bo >= 0 && n >= 2) {
            bo += b[bp].first;
        }
        if (ao > bo) {
            for (int i = 0; i < 2; i++) {
                if (ap >= 0) {
                    ans.push_back(a[ap].second);
                    total += a[ap].first;
                    ap --;
                    n --;
                }
            }
        } else {
            if (bp >= 0) {
                ans.push_back(b[bp].second);
                total += b[bp].first;
                bp --;
                n-=2;
            }
        }
    }
    printf("%lld\n", total);
    for (int i = 0; i < ans.size(); i++) {
        printf("%lld ", ans[i]);
    }
}


//https://leetcode.com/problems/range-sum-query-2d-mutable/
//41  24
#include <bits/stdc++.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
using namespace std;
long long MOD = 1e9+7;
long long M = 550;
long long m, t, k, a, b;
long long t0, t1;
long long n;
int main() {
    vector<long long> ans;
    vector<pair<long long, long long>> a,b;
    scanf("%lld %lld", &k, &n);

    long long tt = 0;
    for (int i = 1; i <= k; i++) {
        scanf("%lld %lld", &t0, &t1);;
        if (t0 == 1) {
            a.push_back({t1, i});
        } else {
            b.push_back({t1, i});
        }
        tt += t1;
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    long long ap = a.size() - 1;
    long long bp = b.size() - 1;
    long long total = 0;
    if (n % 2 == 1 && ap >= 0) {
        ans.push_back(a[ap].second);
        total += a[ap].first;
        ap --;
        n --;
    }

    long long cnt = 0;
    while (n > 0 && (ap >= 0 || bp >= 0)) {
        cnt ++;
        if (cnt > 200000) {
            //printf("[%lld]", n);
            break;
        }
        long long ao = 0;
        long long bo = 0;
        if (ap >= 0) {
            ao += a[ap].first;
        }
        if (ap - 1 >= 0 && n >= 2) {
            ao += a[ap  -1].first;
        }
        if (bo >= 0 && n >= 2) {
            bo += b[bp].first;
        }
        if (ao == 0 && bo == 0) break;
        /*
        if (tt == 4016414 && cnt == 100000) {
            printf("{%d %d}", ao, bo);
        }*/
        if (ao < bo) {
            if (bp >= 0) {
                ans.push_back(b[bp].second);
                total += b[bp].first;
                bp --;
                n-=2;
            }
        } else {
             for (int i = 0; i < 2; i++) {
                if (ap >= 0) {
                    ans.push_back(a[ap].second);
                    total += a[ap].first;
                    ap --;
                    n --;
                }
            }
        }
    }
    //if (4016387 == total) printf("%d\n", (int)ans.size());
    printf("%lld\n", total);
    for (int i = 0; i < ans.size(); i++) {
        printf("%lld ", ans[i]);
    }
}


//https://leetcode.com/problems/range-sum-query-2d-mutable/
//41  24
#include <bits/stdc++.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
using namespace std;
long long MOD = 1e9+7;
long long M = 550;
long long m, t, k, a, b;
long long t0, t1;
long long n;
int main() {
    vector<long long> ans;
    vector<pair<long long, int>> a,b;
    scanf("%lld %lld", &k, &n);

    long long tt = 0;
    for (int i = 1; i <= k; i++) {
        scanf("%lld %lld", &t0, &t1);;
        if (t0 == 1) {
            a.push_back({t1, i});
        } else {
            b.push_back({t1, i});
        }
        tt += t1;
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    int ap = a.size() - 1;
    int bp = b.size() - 1;
    long long total = 0;
    if (n % 2 == 1 && ap >= 0) {
        ans.push_back(a[ap].second);
        total += a[ap].first;
        ap --;
        n --;
    }

    int cnt = 0;
    while (n > 0 && (ap >= 0 || bp >= 0)) {
        cnt ++;
        if (cnt > 200000) {
            printf("[%lld]", n);
            break;
        }
        int ao = 0;
        int bo = 0;
        if (ap >= 0) {
            ao += a[ap].first;
        }
        if (ap - 1 >= 0 && n >= 2) {
            ao += a[ap  -1].first;
        }
        if (bo >= 0 && n >= 2) {
            bo += b[bp].first;
        }
        if (ao == 0 && bo == 0) break;
        /*
        if (tt == 4016414 && cnt == 100000) {
            printf("{%d %d}", ao, bo);
        }*/
        if (ao < bo) {
            if (bp >= 0) {
                ans.push_back(b[bp].second);
                total += b[bp].first;
                bp --;
                n-=2;
            }
        } else {
             for (int i = 0; i < 2; i++) {
                if (ap >= 0) {
                    ans.push_back(a[ap].second);
                    total += a[ap].first;
                    ap --;
                    n --;
                }
            }
        }
    }
    //if (4016387 == total) printf("%d\n", (int)ans.size());
    printf("%lld\n", total);
    for (int i = 0; i < ans.size(); i++) {
        printf("%lld ", ans[i]);
    }
}


//https://leetcode.com/problems/range-sum-query-2d-mutable/
//41  24
#include <bits/stdc++.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
using namespace std;
long long MOD = 1e9+7;
long long M = 550;
long long m, t, k, a, b;
long long t0, t1;
long long n;
int main() {
    vector<long long> ans;
    vector<pair<long long, long long>> a,b;
    scanf("%lld %lld", &k, &n);

    long long tt = 0;
    for (int i = 1; i <= k; i++) {
        scanf("%lld %lld", &t0, &t1);;
        if (t0 == 1) {
            a.push_back({t1, i});
        } else {
            b.push_back({t1, i});
        }
        tt += t1;
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    long long ap = a.size() - 1;
    long long bp = b.size() - 1;
    long long total = 0;
    if (n % 2 == 1 && ap >= 0) {
        ans.push_back(a[ap].second);
        total += a[ap].first;
        ap --;
        n --;
    }

    long long cnt = 0;
    while (n > 0 && (ap >= 0 || bp >= 0)) {
        cnt ++;
        if (cnt > 200000) {
            printf("[%lld]", n);
            break;
        }
        long long ao = 0;
        long long bo = 0;
        if (ap >= 0) {
            ao += a[ap].first;
        }
        if (ap - 1 >= 0 && n >= 2) {
            ao += a[ap  -1].first;
        }
        if (bo >= 0 && n >= 2) {
            bo += b[bp].first;
        }
        if (ao == 0 && bo == 0) break;

        if (tt == 4016414) {
            printf("%d %d %d, %d %d|", ao, bo, n, ap, bp);
        }
        if (ao < bo) {
            if (bp >= 0) {
                ans.push_back(b[bp].second);
                total += b[bp].first;
                bp --;
                n-=2;
            }
        } else {
             for (int i = 0; i < 2; i++) {
                if (ap >= 0) {
                    ans.push_back(a[ap].second);
                    total += a[ap].first;
                    ap --;
                    n --;
                }
            }
        }
    }
    //if (4016387 == total) printf("%d\n", (int)ans.size());
    printf("%lld\n", total);
    for (int i = 0; i < ans.size(); i++) {
        printf("%lld ", ans[i]);
    }
}


//https://leetcode.com/problems/range-sum-query-2d-mutable/
//41  24
#include <bits/stdc++.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
using namespace std;
long long MOD = 1e9+7;
long long M = 550;
long long m, t, k, a, b;
long long t0, t1;
long long n;
int main() {
    vector<long long> ans;
    vector<pair<long long, long long>> a,b;
    scanf("%lld %lld", &k, &n);

    long long tt = 0;
    for (int i = 1; i <= k; i++) {
        scanf("%lld %lld", &t0, &t1);;
        if (t0 == 1) {
            a.push_back({t1, i});
        } else {
            b.push_back({t1, i});
        }
        tt += t1;
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    long long ap = - 1 + (int)a.size() ;
    long long bp = -1 +(int)b.size();
    long long total = 0;
    if (n % 2 == 1 && ap >= 0) {
        ans.push_back(a[ap].second);
        total += a[ap].first;
        ap --;
        n --;
    }

    long long cnt = 0;
    while (n > 0 && (ap >= 0 || bp >= 0)) {
        cnt ++;
        if (cnt > 200000) {
            printf("[%lld]", n);
            break;
        }
        long long ao = 0;
        long long bo = 0;
        if (ap >= 0) {
            ao += a[ap].first;
        }
        if (ap - 1 >= 0 && n >= 2) {
            ao += a[ap  -1].first;
        }
        if (bo >= 0 && n >= 2) {
            bo += b[bp].first;
        }
        if (ao == 0 && bo == 0) break;

        if (tt == 4016414) {
            //printf("%lld %lld %lld, %lld %lld|", ao, bo, n, ap, bp);
        }
        if (ao < bo) {
            if (bp >= 0) {
                ans.push_back(b[bp].second);
                total += b[bp].first;
                bp --;
                n-=2;
            }
        } else {
             for (int i = 0; i < 2; i++) {
                if (ap >= 0) {
                    ans.push_back(a[ap].second);
                    total += a[ap].first;
                    ap --;
                    n --;
                }
            }
        }
    }
    //if (4016387 == total) printf("%d\n", (int)ans.size());
    printf("%lld\n", total);
    for (int i = 0; i < ans.size(); i++) {
        printf("%lld ", ans[i]);
    }
}


#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<int, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 0, x, y; i < n; i ++) {
        cin >> x >> y;
        if(x == 1)
            a.push_back(make_pair(y, i + 1));
        else
            b.push_back(make_pair(y, i + 1));
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    int la = a.size();
    int lb = b.size();
    for(int i = 1; i < la; i ++) a[i].first += a[i - 1].first;
    for(int i = 1; i < lb; i ++) b[i].first += b[i - 1].first;
    int ms = 0, mi = 0, mj = 0;
    for(int j = 0; j <= lb; j ++) {
        int vj = j;
        if(2 * vj > v) break;
        int vi = v - 2 * vj;
        if(vi < 0) continue;
        //cout << "-- " << vi << " " << vj << " --\n";
        int vs = 0;
        if(vi > 0) vs += a[vi - 1].first;
        if(vj > 0) vs += b[vj - 1].first;
        if(ms < vs) {
            ms = vs;
            mi = vi;
            mj = vj;
        }
    }
    cout << ms << endl;
    for(int i = 0; i < mi; i ++) cout << a[i].second << " ";
    for(int i = 0; i < mj; i ++) cout << b[i].second << " ";

    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#define pii pair<int, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 1; i <= n; i ++) {
        int x, y;
        cin >> x >> y;
        if(x == 2) {
            a.push_back({y, i});
        } else {
            b.push_back({y, i});
        }
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    int la = a.size();
    int lb = b.size();
    vector<int> res;
    long long sum = 0;
    while(la > 0 && v > 1) {
        la --;
        res.push_back(a[la].second);
        sum += a[la].first;
        v -= 2;
    }
    while(lb > 0 && v > 0) {
        lb --;
        res.push_back(b[lb].second);
        sum += b[lb].first;
        v --;
    }
    cout << sum << endl;
    int l = res.size();
    for(int i = 0; i < l; i ++) {
        cout << res[i] << " ";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<int, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 0, x, y; i < n; i ++) {
        cin >> x >> y;
        if(x == 1)
            a.push_back(make_pair(y, i + 1));
        else
            b.push_back(make_pair(y, i + 1));
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    vector<int> res;
    int sum = 0;
    int la = a.size(), lb = b.size();
    int i = 0, j = 0;
    if(v % 2) {
        sum += a[0].first;
        res.push_back(a[0].second);
        i ++;
        v --;
    }
    while((i < la || j < lb)) {
        if(i >= la - 1) {
            sum += b[j].first; res.push_back(b[j].second); j ++; v -= 2;
        } else if(j >= lb) {
            if(i < la) {sum += a[i].first; res.push_back(a[i].second); i ++; v --;}
            if(i < la) {sum += a[i].first; res.push_back(a[i].second); i ++; v --;}
        } else {
            int s1 = a[i].first + a[i + 1].first; int s2 = b[j].first;
            if(s1 > s2) {
            if(i < la) {sum += a[i].first; res.push_back(a[i].second); i ++; v --;}
            if(i < la) {sum += a[i].first; res.push_back(a[i].second); i ++; v --;}
            } else {
                sum += b[j].first; res.push_back(b[j].second); j ++; v -= 2;
            }
        }
    }
    cout << sum << endl;
    int l = res.size();
    for(int i = 0; i < l; i ++) {
        cout << res[i] << " ";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<int, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 0, x, y; i < n; i ++) {
        cin >> x >> y;
        if(x == 1)
            a.push_back(make_pair(y, i + 1));
        else
            b.push_back(make_pair(y, i + 1));
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    vector<int> res;
    int sum = 0;
    int la = a.size(), lb = b.size();
    int i = 0, j = 0;
    if(v % 2) {
        sum += a[0].first;
        res.push_back(a[0].second);
        i ++;
        v --;
    }
    while(v > 0) {
        if(i >= la - 1) {
            sum += b[j].first; res.push_back(b[j].second); j ++; v -= 2;
        } else if(j >= lb) {
            sum += a[i].first; res.push_back(a[i].second); i ++;
            sum += a[i].first; res.push_back(a[i].second); i ++; v -= 2;
        } else {
            int s1 = a[i].first + a[i + 1].first; int s2 = b[j].first;
            if(s1 > s2) {
                sum += a[i].first; res.push_back(a[i].second); i ++;
                sum += a[i].first; res.push_back(a[i].second); i ++; v -= 2;
            } else {
                sum += b[j].first; res.push_back(b[j].second); j ++; v -= 2;
            }
        }
    }
    cout << sum << endl;
    int l = res.size();
    for(int i = 0; i < l; i ++) {
        cout << res[i] << " ";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<int, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 0, x, y; i < n; i ++) {
        cin >> x >> y;
        if(x == 1)
            a.push_back(make_pair(y, i + 1));
        else
            b.push_back(make_pair(y, i + 1));
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    int la = a.size();
    int lb = b.size();
    for(int i = 1; i < la; i ++) a[i].first += a[i - 1].first;
    for(int i = 1; i < lb; i ++) b[i].first += b[i - 1].first;
    long long ms = 0, mi = 0, mj = 0;
    for(int i = 0; i <= la; i ++) {
        int vi = i;
        if(vi > v) break;
        if((v - vi) % 2) continue;
        int vj = (v - vi) / 2;
        if(vj > lb) continue;
        //cout << " -- " << vi << " " << vj << "\n";
        long long vs = 0;
        if(vi > 0) vs += a[vi - 1].first;
        if(vj > 0) vs += b[vj - 1].first;
        if(vs >= ms) {
            ms = vs;
            mi = vi;
            mj = vj;
        }
    }
    cout << ms << endl;
    for(int i = 0; i < mi; i ++) cout << a[i].second << " ";
    for(int i = 0; i < mj; i ++) cout << b[i].second << " ";

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<long long, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 0, x, y; i < n; i ++) {
        cin >> x >> y;
        if(x == 1)
            a.push_back(make_pair(y, i + 1));
        else
            b.push_back(make_pair(y, i + 1));
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    int la = a.size();
    int lb = b.size();
    for(int i = 1; i < la; i ++) a[i].first += a[i - 1].first;
    for(int i = 1; i < lb; i ++) b[i].first += b[i - 1].first;
    long long ms = 0, mi = 0, mj = 0;
    for(int i = 0; i <= la; i ++) {
        int vi = i;
        if(vi > v) break;
        if((v - vi) % 2) continue;
        int vj = (v - vi) / 2;
        if(vj > lb) continue;
        //cout << " -- " << vi << " " << vj << "\n";
        long long vs = 0;
        if(vi > 0) vs += a[vi - 1].first;
        if(vj > 0) vs += b[vj - 1].first;
        if(vs >= ms) {
            ms = vs;
            mi = vi;
            mj = vj;
        }
    }
    cout << ms << endl;
    for(int i = 0; i < mi; i ++) cout << a[i].second << " ";
    for(int i = 0; i < mj; i ++) cout << b[i].second << " ";

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<int, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 0, x, y; i < n; i ++) {
        cin >> x >> y;
        if(x == 1)
            a.push_back(make_pair(y, i + 1));
        else
            b.push_back(make_pair(y, i + 1));
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    vector<int> res;
    int sum = 0;
    int la = a.size(), lb = b.size();
    int i = 0, j = 0;
    if(v % 2) {
        sum += a[0].first;
        res.push_back(a[0].second);
        i ++;
        v --;
    }
    while(v > 0) {
        if(j < lb && i >= la - 1) {
            sum += b[j].first; res.push_back(b[j].second); j ++;
        } else if(i < la - 1 && j >= lb) {
            sum += a[i].first; res.push_back(a[i].second); i ++;
            sum += a[i].first; res.push_back(a[i].second); i ++;
        } else {
            int s1 = a[i].first + a[i + 1].first; int s2 = b[j].first;
            if(s1 > s2) {
                sum += a[i].first; res.push_back(a[i].second); i ++;
                sum += a[i].first; res.push_back(a[i].second); i ++;
            } else {
                sum += b[j].first; res.push_back(b[j].second); j ++;
            }
        }
        v -= 2;
    }
    cout << sum << endl;
    int l = res.size();
    for(int i = 0; i < l; i ++) {
        cout << res[i] << " ";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<int, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 0, x, y; i < n; i ++) {
        cin >> x >> y;
        if(x == 1)
            a.push_back(make_pair(y, i + 1));
        else
            b.push_back(make_pair(y, i + 1));
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    vector<int> res;
    int sum = 0;
    int la = a.size(), lb = b.size();
    int i = 0, j = 0;
    if(v % 2) {
        sum += a[0].first;
        res.push_back(a[0].second);
        i ++;
        v --;
    }
    while(v > 0) {
        if(j < lb && i >= la - 1) {
            sum += b[j].first; res.push_back(b[j].second); j ++;
        } else if(i < la - 1 && j >= lb)) {
            sum += a[i].first; res.push_back(a[i].second); i ++;
            sum += a[i].first; res.push_back(a[i].second); i ++;
        } else {
            int s1 = a[i].first + a[i + 1].first; int s2 = b[j].first;
            if(s1 > s2) {
                sum += a[i].first; res.push_back(a[i].second); i ++;
                sum += a[i].first; res.push_back(a[i].second); i ++;
            } else {
                sum += b[j].first; res.push_back(b[j].second); j ++;
            }
        }
        v -= 2;
    }
    cout << sum << endl;
    int l = res.size();
    for(int i = 0; i < l; i ++) {
        cout << res[i] << " ";
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#define pii pair<int, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 1; i <= n; i ++) {
        int x, y;
        cin >> x >> y;
        if(x == 2) {
            a.push_back({y, i});
        } else {
            b.push_back({y, i});
        }
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    int la = a.size();
    int lb = b.size();
    vector<int> res;
    long long sum = 0;

    for(int i = la - 1, j = lb - 1; v > 0;) {
        if(v > 1) {
            if(i >= 0) {
                if(j > 0) {
                    if(a[i].first >= b[j].first + b[j - 1].first) {
                        sum += a[i].first;
                        res.push_back(a[i --].second);
                        v -= 2;
                    } else {
                        sum += b[j].first + b[j - 1].first;
                        res.push_back(b[j --].second);
                        res.push_back(b[j --].second);
                        v -= 2;
                    }
                } else {
                    sum += a[i].first;
                    res.push_back(a[i --].second);
                    v -= 2;
                }
            } else {
                sum += b[j].first + b[j - 1].first;
                res.push_back(b[j --].second);
                res.push_back(b[j --].second);
                v -= 2;
            }

        } else {
            sum += b[j].first;
            res.push_back(b[j].second);
            v --;
        }
    }

    cout << sum << endl;
    int l = res.size();
    for(int i = 0; i < l; i ++) {
        cout << res[i] << " ";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<int, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 0, x, y; i < n; i ++) {
        cin >> x >> y;
        if(x == 1)
            a.push_back(make_pair(y, i + 1));
        else
            b.push_back(make_pair(y, i + 1));
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    vector<int> res;
    long long sum = 0;
    int la = a.size(), lb = b.size();
    int i = 0, j = 0;
    while(v - 2 >= 0) {
        if(j < lb && i >= la - 1) {
            sum += b[j].first; res.push_back(b[j].second); j ++;
        } else if(i < la - 1 && j >= lb) {
            sum += a[i].first; res.push_back(a[i].second); i ++;
            sum += a[i].first; res.push_back(a[i].second); i ++;
        } else {
            int s1 = a[i].first + a[i + 1].first; int s2 = b[j].first;
            if(s1 > s2) {
                sum += a[i].first; res.push_back(a[i].second); i ++;
                sum += a[i].first; res.push_back(a[i].second); i ++;
            } else {
                sum += b[j].first; res.push_back(b[j].second); j ++;
            }
        }
        v -= 2;
    }
    if(v > 0) {
        sum += a[i].first;
        res.push_back(a[i].second);
        i ++;
        v --;
    }

    cout << sum << endl;
    int l = res.size();
    for(int i = 0; i < l; i ++) {
        cout << res[i] << " ";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<long long, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 0, x, y; i < n; i ++) {
        cin >> x >> y;
        if(x == 1)
            a.push_back(make_pair(y, i + 1));
        else
            b.push_back(make_pair(y, i + 1));
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    int la = a.size();
    int lb = b.size();
    for(int i = 1; i < la; i ++) a[i].first += a[i - 1].first;
    for(int i = 1; i < lb; i ++) b[i].first += b[i - 1].first;
    long long ms = 0, mi = 0, mj = 0;
    for(int i = 0; i <= lb; i ++) {
        if(2 * i <= v) {
            int vj = i;
            int vi = min(la, v - 2 * vj);
            int vs = (vi ? a[vi - 1].first : 0) + (vj ? b[vj - 1].first : 0);
            if(vs > ms) {
                ms = vs;
                mi = vi;
                mj = vj;
            }
        }
    }
    cout << ms << endl;
    for(int i = 0; i < mi; i ++) cout << a[i].second << " ";
    for(int i = 0; i < mj; i ++) cout << b[i].second << " ";

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<long long, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 0, x, y; i < n; i ++) {
        cin >> x >> y;
        if(x == 1)
            a.push_back(make_pair(y, i + 1));
        else
            b.push_back(make_pair(y, i + 1));
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    int la = a.size();
    int lb = b.size();
    for(int i = 1; i < la; i ++) a[i].first += a[i - 1].first;
    for(int i = 1; i < lb; i ++) b[i].first += b[i - 1].first;
    long long ms = 0, mi = 0, mj = 0;
    for(int i = 0; i <= la; i ++) {
        if(i <= v && (v - i) % 2 == 0 && (v - i) / 2 >= 0) {
            int vi = i;
            int vj = min(lb, (v - vi) / 2);
            int vs = (vi ? a[vi - 1].first : 0) + (vj ? b[vj - 1].first : 0);
            if(vs > ms) {
                ms = vs;
                mi = vi;
                mj = vj;
            }
        }
    }
    cout << ms << endl;
    for(int i = 0; i < mi; i ++) cout << a[i].second << " ";
    for(int i = 0; i < mj; i ++) cout << b[i].second << " ";

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<int, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 0, x, y; i < n; i ++) {
        cin >> x >> y;
        if(x == 1)
            a.push_back(make_pair(y, i + 1));
        else
            b.push_back(make_pair(y, i + 1));
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    int la = a.size();
    int lb = b.size();
    for(int i = 1; i < la; i ++) a[i].first += a[i - 1].first;
    for(int i = 1; i < lb; i ++) b[i].first += b[i - 1].first;
    int ms = 0, mi = 0, mj = 0;
    for(int j = 0; j <= lb; j ++) {
        int vj = j;
        if(2 * vj > v) break;
        int vi = v - 2 * vj;
        if(vi < 0 || vi > la) continue;
//        cout << "-- " << vi << " " << vj << " --\n";
        int vs = 0;
        if(vi > 0) vs += a[vi - 1].first;
        if(vj > 0) vs += b[vj - 1].first;
        if(ms < vs) {
            ms = vs;
            mi = vi;
            mj = vj;
        }
    }
    cout << ms << endl;
    for(int i = 0; i < mi; i ++) cout << a[i].second << " ";
    for(int i = 0; i < mj; i ++) cout << b[i].second << " ";

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<long long, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 0, x, y; i < n; i ++) {
        cin >> x >> y;
        if(x == 1)
            a.push_back(make_pair(y, i + 1));
        else
            b.push_back(make_pair(y, i + 1));
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    int la = a.size();
    int lb = b.size();
    for(int i = 1; i < la; i ++) a[i].first += a[i - 1].first;
    for(int i = 1; i < lb; i ++) b[i].first += b[i - 1].first;
    long long ms = 0, mi = 0, mj = 0;
    for(int i = 0; i <= min(la, v); i ++) {
        int vi = i;
        if((v - vi) % 2) continue;
        int vj = (v - vi) / 2;
        if(vj > lb) vj = lb;
        //cout << " -- " << vi << " " << vj << "\n";
        long long vs = 0;
        if(vi > 0) vs += a[vi - 1].first;
        if(vj > 0) vs += b[vj - 1].first;
        if(vs >= ms) {
            ms = vs;
            mi = vi;
            mj = vj;
        }
    }
    cout << ms << endl;
    for(int i = 0; i < mi; i ++) cout << a[i].second << " ";
    for(int i = 0; i < mj; i ++) cout << b[i].second << " ";

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<int, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 0, x, y; i < n; i ++) {
        cin >> x >> y;
        if(x == 1)
            a.push_back(make_pair(y, i + 1));
        else
            b.push_back(make_pair(y, i + 1));
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    vector<int> res;
    long long sum = 0;
    int la = a.size(), lb = b.size();
    int i = 0, j = 0;
    if(v % 2) {
        sum += a[0].first;
        res.push_back(a[0].second);
        i ++;
        v --;
    }
    while(v > 0) {
        if(j < lb && i >= la - 1) {
            sum += b[j].first; res.push_back(b[j].second); j ++;
        } else if(i < la - 1 && j >= lb) {
            sum += a[i].first; res.push_back(a[i].second); i ++;
            sum += a[i].first; res.push_back(a[i].second); i ++;
        } else {
            int s1 = a[i].first + a[i + 1].first; int s2 = b[j].first;
            if(s1 > s2) {
                sum += a[i].first; res.push_back(a[i].second); i ++;
                sum += a[i].first; res.push_back(a[i].second); i ++;
            } else {
                sum += b[j].first; res.push_back(b[j].second); j ++;
            }
        }
        v -= 2;
    }
    cout << sum << endl;
    int l = res.size();
    for(int i = 0; i < l; i ++) {
        cout << res[i] << " ";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<long long, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 0, x, y; i < n; i ++) {
        cin >> x >> y;
        if(x == 1)
            a.push_back(make_pair(y, i + 1));
        else
            b.push_back(make_pair(y, i + 1));
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    int la = a.size();
    int lb = b.size();
    for(int i = 1; i < la; i ++) a[i].first += a[i - 1].first;
    for(int i = 1; i < lb; i ++) b[i].first += b[i - 1].first;
    long long ms = 0, mi = 0, mj = 0;
    for(int i = 0; i <= la; i ++) {
        if(la <= v && (v - la) % 2 == 0) {
            int vi = i;
            int vj = min(lb, (v - vi) / 2);
            int vs = (vi ? a[vi - 1].first : 0) + (vj ? b[vj - 1].first : 0);
            if(vs > ms) {
                ms = vs;
                mi = vi;
                mj = vj;
            }
        }
    }
    cout << ms << endl;
    for(int i = 0; i < mi; i ++) cout << a[i].second << " ";
    for(int i = 0; i < mj; i ++) cout << b[i].second << " ";

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<long long, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 0, x, y; i < n; i ++) {
        cin >> x >> y;
        if(x == 1)
            a.push_back(make_pair(y, i + 1));
        else
            b.push_back(make_pair(y, i + 1));
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    int la = a.size();
    int lb = b.size();
    for(int i = 1; i < la; i ++) a[i].first += a[i - 1].first;
    for(int i = 1; i < lb; i ++) b[i].first += b[i - 1].first;
    long long ms = 0, mi = 0, mj = 0;
    for(int i = 0; i <= la; i ++) {
        if(la <= v && (v - i) % 2 == 0 && (v - i) / 2 >= 0) {
            int vi = i;
            int vj = min(lb, (v - vi) / 2);
            int vs = (vi ? a[vi - 1].first : 0) + (vj ? b[vj - 1].first : 0);
            if(vs > ms) {
                ms = vs;
                mi = vi;
                mj = vj;
            }
        }
    }
    cout << ms << endl;
    for(int i = 0; i < mi; i ++) cout << a[i].second << " ";
    for(int i = 0; i < mj; i ++) cout << b[i].second << " ";

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<int, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 0, x, y; i < n; i ++) {
        cin >> x >> y;
        if(x == 1)
            a.push_back(make_pair(y, i + 1));
        else
            b.push_back(make_pair(y, i + 1));
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    vector<int> res;
    int sum = 0;
    int la = a.size(), lb = b.size();
    int i = 0, j = 0;
    if(v % 2) {
        sum += a[0].first;
        res.push_back(a[0].second);
        i ++;
        v --;
    }
    while((i < la || j < lb) && v > 0) {
        if(i >= la - 1) {
            sum += b[j].first; res.push_back(b[j].second); j ++; v -= 2;
        } else if(j >= lb) {
            if(i < la) {sum += a[i].first; res.push_back(a[i].second); i ++; v --;}
            if(i < la) {sum += a[i].first; res.push_back(a[i].second); i ++; v --;}
        } else {
            int s1 = a[i].first + a[i + 1].first; int s2 = b[j].first;
            if(s1 > s2) {
            if(i < la) {sum += a[i].first; res.push_back(a[i].second); i ++; v --;}
            if(i < la) {sum += a[i].first; res.push_back(a[i].second); i ++; v --;}
            } else {
                sum += b[j].first; res.push_back(b[j].second); j ++; v -= 2;
            }
        }
    }
    cout << sum << endl;
    int l = res.size();
    for(int i = 0; i < l; i ++) {
        cout << res[i] << " ";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<int, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 0, x, y; i < n; i ++) {
        cin >> x >> y;
        if(x == 1)
            a.push_back(make_pair(y, i + 1));
        else
            b.push_back(make_pair(y, i + 1));
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    int la = a.size();
    int lb = b.size();
    for(int i = 1; i < la; i ++) a[i].first += a[i - 1].first;
    for(int i = 1; i < lb; i ++) b[i].first += b[i - 1].first;
    int ms = 0, mi = 0, mj = 0;
    for(int j = 0; j < lb; j ++) {
        int vj = j + 1;
        if(2 * vj > v) break;
        int vi = v - 2 * vj;
        if(vi < 0) continue;
        int vs = 0;
        if(vi > 0) vs += a[vi - 1].first;
        if(vj > 0) vs += b[vj - 1].first;
        if(ms < vs) {
            ms = vs;
            mi = vi;
            mj = vj;
        }
    }
    cout << ms << endl;
    for(int i = 0; i < mi; i ++) cout << a[i].second << " ";
    for(int i = 0; i < mj; i ++) cout << b[i].second << " ";

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<int, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 0, x, y; i < n; i ++) {
        cin >> x >> y;
        if(x == 1)
            a.push_back(make_pair(y, i + 1));
        else
            b.push_back(make_pair(y, i + 1));
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    vector<int> res;
    long long sum = 0;
    int la = a.size(), lb = b.size();
    int i = 0, j = 0;
    while(v - 2 >= 0) {
        if(j < lb && i >= la - 1) {
            sum += b[j].first; res.push_back(b[j].second); j ++;
        } else if(i < la - 1 && j >= lb) {
            sum += a[i].first; res.push_back(a[i].second); i ++;
            sum += a[i].first; res.push_back(a[i].second); i ++;
        } else {
            int s1 = a[i].first + a[i + 1].first; int s2 = b[j].first;
            if(s1 > s2) {
                sum += a[i].first; res.push_back(a[i].second); i ++;
                sum += a[i].first; res.push_back(a[i].second); i ++;
            } else {
                sum += b[j].first; res.push_back(b[j].second); j ++;
            }
        }
        v -= 2;
    }
    if(v > 0) {
        sum += a[0].first;
        res.push_back(a[0].second);
        i ++;
        v --;
    }

    cout << sum << endl;
    int l = res.size();
    for(int i = 0; i < l; i ++) {
        cout << res[i] << " ";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<int, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 0, x, y; i < n; i ++) {
        cin >> x >> y;
        if(x == 1)
            a.push_back(make_pair(y, i + 1));
        else
            b.push_back(make_pair(y, i + 1));
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    vector<int> res;
    int sum = 0;
    int la = a.size(), lb = b.size();
    int i = 0, j = 0;
    if(v % 2) {
        sum += a[0].first;
        res.push_back(a[0].second);
        i ++;
        v --;
    }
    while(v > 0) {
        if(i >= la - 2) {
            sum += b[j].first; res.push_back(b[j].second); j ++; v -= 2;
        } else if(j >= lb) {
            sum += a[i].first; res.push_back(a[i].second); i ++;
            sum += a[i].first; res.push_back(a[i].second); i ++; v -= 2;
        } else {
            int s1 = a[i].first + a[i + 1].first; int s2 = b[j].first;
            if(s1 > s2) {
                sum += a[i].first; res.push_back(a[i].second); i ++;
                sum += a[i].first; res.push_back(a[i].second); i ++; v -= 2;
            } else {
                sum += b[j].first; res.push_back(b[j].second); j ++; v -= 2;
            }
        }
    }
    cout << sum << endl;
    int l = res.size();
    for(int i = 0; i < l; i ++) {
        cout << res[i] << " ";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<long long, int>
using namespace std;
vector<pii> a, b;
int main() {
    int n, v;
    cin >> n >> v;
    for(int i = 0, x, y; i < n; i ++) {
        cin >> x >> y;
        if(x == 1)
            a.push_back(make_pair(y, i + 1));
        else
            b.push_back(make_pair(y, i + 1));
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    int la = a.size();
    int lb = b.size();
    for(int i = 1; i < la; i ++) a[i].first += a[i - 1].first;
    for(int i = 1; i < lb; i ++) b[i].first += b[i - 1].first;
    long long ms = 0, mi = 0, mj = 0;
    for(int i = 0; i <= la; i ++) {
        int vi = i;
        if(vi > v) break;
        if((v - vi) % 2) continue;
        int vj = (v - vi) / 2;
        if(vj > lb) vj = lb;
        //cout << " -- " << vi << " " << vj << "\n";
        long long vs = 0;
        if(vi > 0) vs += a[vi - 1].first;
        if(vj > 0) vs += b[vj - 1].first;
        if(vs >= ms) {
            ms = vs;
            mi = vi;
            mj = vj;
        }
    }
    cout << ms << endl;
    for(int i = 0; i < mi; i ++) cout << a[i].second << " ";
    for(int i = 0; i < mj; i ++) cout << b[i].second << " ";

    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;
                minns=t1[i1-1].first;

            }
            else
            {
                s1=t1[i1].first;
                minns=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;
                ans[ai++]=t1[i1].second;
                indmin=ai;
                ans[ai++]=t1[i1-1].second;
                i1-=2;}
                else
                {
                    sum+=s1;
                    s--;
                    indmin=ai;
                    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 && j1>=0)
    {
        if(minns < t2[j1].first)
        {
            sum+= t2[j1].first - minns;
            ans[indmin]=t2[j1].second;
            s--;
        }
    }
    cout << sum << endl;
    for(int i=0;i<ai;i++)
    {
        cout << ans[i]+1 <<" ";
    }
    cout << endl;

    return 0;
}