#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int n, m, t, i, j, z, b[105], x;
int a[105];
main(){
    cin >> t;
    for (i= 1; i<=t; ++i){
        cin >> n >> m;
        for (j = 1; j<=n; ++j) {cin >> a[j];}
        for (j = 1; j<=m; ++j) {b[j]=0;}
        for (j = 1; j<=m; ++j) {cin >> x; b[x]++;}
int f = 0;
        for(j = 1; j<=n+n; ++j)
            for(z = 1; z<n; ++z)
                if (a[z]>a[z+1]){
                    if (b[z]==0) f = 1;
                    swap(a[z], a[z+1]);
                }
        for (j = 1; j<n; ++j)
            if (a[j]>a[j+1]) ++f;
        if(f) cout <<"NO\n";
        else cout <<"YES\n";
    }
}


#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int n, m, t, i, j, z, b[105], x;
int a[105];
main(){
    cin >> t;
    for (i= 1; i<=t; ++i){
        cin >> n >> m;
        for (j = 1; j<=n; ++j) {cin >> a[j];}
        for (j = 1; j<=m; ++j) {b[j]=0;}
        for (j = 1; j<=m; ++j) {cin >> x; b[x]++;}
int f = 0;
        for(j = 1; j<=n+n; ++j)
            for(z = 1; z<n; ++z)
                if (a[z]>a[z+1]){
                    if (b[z]==0) f = 1;
                    swap(a[z], a[z+1]);
                }
        for (j = 1; j<n; ++j)
            if (a[z]>a[z+1]) ++f;
        if(f) cout <<"NO\n";
        else cout <<"YES\n";
    }
}


#include <iostream>

using namespace std;

int main()
{
    int t;
    cin>>t;
    while (t--){
        int n,m;
        cin>>n>>m;
        int arr[n+1]={0};
        bool brr[n+1]={0};
        for (int i=1;i<=n;i++)
            cin>>arr[i];
        for (int i=0;i<m;i++){
            int x;
            cin>>x;
            brr[x]=true;
        }
        for (int i=0;i<=n;i++){
            if (brr[arr[i]]==true){
                int j=i;
                while (arr[j]<=arr[j-1]){
                    swap(arr[j],arr[j-1]);
                    j--;
                }
            }
        }
        bool check=true;
        for (int i=1;i<=n;i++){
            if (arr[i]<arr[i-1]){
                cout<<"NO";
                check=false;
                break;
            }
        }
        if (check)
            cout<<"YES";
        cout<<"\n";

    }

    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int t;
    cin>>t;
    while (t--){
        int n,m,k=0;
        vector <pair<int,int> > vect;
        cin>>n>>m;
        int arr[n];
        bool brr[105]={0};
        for (int i=0;i<n;i++)
            cin>>arr[i];
        for (int i=0;i<m;i++){
            int x;
            cin>>x;
            brr[x]=true;
        }
    bool check=true;
     int maxx=arr[0],minn=arr[0];
       for (int i=0;i<n;i++){
            if (brr[i]==false && i!=0){
                vect.push_back({minn,maxx});
                maxx=0,minn=105;
            }
            maxx=max(maxx,arr[i]);
            minn=min(minn,arr[i]);

       }
    vect.push_back({minn,maxx});
    for (int i=0;i<vect.size()-1;i++){
        if (vect[i].second>vect[i+1].first)
            check=false;
    }
       if (check)
            cout<<"YES";
       else
            cout<<"NO";
       cout<<"\n";
    }

    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int t;
    cin>>t;
    while (t--){
        int n,m;
        cin>>n>>m;
        int arr[n+1]={0};
        bool brr[105]={0};
        for (int i=1;i<=n;i++)
            cin>>arr[i];
        for (int i=0;i<m;i++){
            int x;
            cin>>x;
            brr[x]=true;
        }
        for (int i=0;i<=n;i++){
            if (brr[arr[i]]==true){
                int j=i;
                while (arr[j]<=arr[j-1]){
                    swap(arr[j],arr[j-1]);
                    j--;
                }
            }
        }
        bool check=true;
        for (int i=1;i<=n;  i++){
            if (arr[i]<arr[i-1]){
                cout<<"NO";
                check=false;
                break;
            }
        }
        if (check)
            cout<<"YES";
        cout<<"\n";

    }

    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int t;
    cin>>t;
    while (t--){
        int n,m,k=0;
        cin>>n>>m;
        int arr[n+1]={0};
        bool brr[105]={0};
        for (int i=1;i<=n;i++)
            cin>>arr[i];
        for (int i=0;i<m;i++){
            int x;
            cin>>x;
            brr[x]=true;
        }
        bool check=true;
       for (int i=0;i<=n;i++){
        if (brr[arr[i]]==false && arr[i]>=k)
            k=arr[i];
        else if (brr[arr[i]]==false)
            check=false;
       }
       if (check)
            cout<<"YES";
       else
            cout<<"NO";
       cout<<"\n";
    }

    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int t;
    cin>>t;
    while (t--){
        int n,m,k=0;
        cin>>n>>m;
        int arr[n];
        bool brr[n]={0};
        for (int i=0;i<n;i++)
            cin>>arr[i];
        for (int i=0;i<m;i++){
            int x;
            cin>>x;
            brr[x-1]=true;
        }
        for (int i=0;i<n;i++){
            if (!brr[i])
                continue;
            int j=i;
            while (j<n && brr[j])
                j++;
            sort(arr+i,arr+j+1);

        }
        bool check=true;
        for (int i=0;i<n-1;i++)
            check &= arr[i]<=arr[i+1];
        if (check)
            cout<<"YES";
        else
            cout<<"NO";
        cout<<"\n";
    }

    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int t;
    cin>>t;
    while (t--){
        int n,m,k=0;
        vector <pair<int,int> > vect;
        cin>>n>>m;
        int arr[n];
        bool brr[n]={0};
        for (int i=0;i<n;i++)
            cin>>arr[i];
        for (int i=0;i<m;i++){
            int x;
            cin>>x;
            brr[x-1]=true;
        }
        for (int i=0;i<n;i++){
            if (!brr[i])
                continue;
            int j=i;
            while (j<n && brr[j])
                j++;
            sort(arr+i,arr+j+1);

        }
        bool check=true;
        for (int i=0;i<n-1;i++)
            check &= arr[i]<=arr[i+1];
        if (check)
            cout<<"YES";
        else
            cout<<"NO";
        cout<<"\n";
    }

    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int t;
    cin>>t;
    while (t--){
        int n,m,k=0;
        cin>>n>>m;
        int arr[n+1]={0};
        bool brr[105]={0};
        for (int i=1;i<=n;i++)
            cin>>arr[i];
        for (int i=0;i<m;i++){
            int x;
            cin>>x;
            brr[x]=true;
        }
        bool check=true;
       for (int i=0;i<=n;i++){
        if (brr[i]==false && arr[i]>=k)
            k=arr[i];
        else if (brr[i]==false)
            check=false;
       }
       if (check)
            cout<<"YES";
       else
            cout<<"NO";
       cout<<"\n";
    }

    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int t;
    cin>>t;
    while (t--){
        int n,m,k=0;
        cin>>n>>m;
        int arr[n];
        bool brr[105]={0};
        for (int i=0;i<n;i++)
            cin>>arr[i];
        for (int i=0;i<m;i++){
            int x;
            cin>>x;
            brr[x+1]=true;
        }
     int maxx=0;
    bool check=true;
       for (int i=0;i<n;i++){
            if (brr[i+1]==false && maxx>arr[i])
                    check=false;
            else
                maxx=max(maxx,arr[i]);

       }
       if (check)
            cout<<"YES";
       else
            cout<<"NO";
       cout<<"\n";
    }

    return 0;
}


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

int main(void)
{
	int t;
	cin >> t;

	while(t--)
	{
		int n, m;
		cin >> n >> m;

		string s = "0";
		string goal = "0";
		int visited[110] = {0};

		for(int i = 1; i <= n; i++)
		{
			int input;
			cin >> input;

			s += to_string(input);
			goal += to_string(input);
		}
		sort(goal.begin(), goal.end());

		for(int i = 1; i <= m; i++)
		{
			int input;
			cin >> input;

			visited[input] = 1;
		}

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

			while(visited[j] == 1)
			{
				j++;
			}

			sort(s.begin()+i, s.begin()+j+1);

			i = j;
		}

		if(s == goal)
		{
			cout << "YES\n";
		}
		else
		{
			cout << "NO\n";
		}
	}

	return 0;
}


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

int main(void)
{
	int t;
	cin >> t;

	while(t--)
	{
		int n, m;
		cin >> n >> m;

		string s;
		string goal;
		int visited[110] = {0};

		for(int i = 1; i <= n; i++)
		{
			int input;
			cin >> input;

			s += to_string(input);
			goal += to_string(input);
		}
		sort(goal.begin(), goal.end());

		for(int i = 1; i <= m; i++)
		{
			int input;
			cin >> input;

			visited[--input] = 1;
		}

		for(int i = 0; i < n; i++)
		{
			if(visited[i])
			{
				int j = i;

				while(visited[j] == 1)
				{
					j++;
				}

				sort(s.begin()+i, s.begin()+j+1);

				i = j;
			}
		}

		if(s == goal)
		{
			cout << "YES\n";
		}
		else
		{
			cout << "NO\n";
		}
	}

	return 0;
}


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

bool flag = false;

void swap(string &now, int a, int b)
{
	char temp = now[a];
	now[a] = now[b];
	now[b] = temp;
}

void change(string now, string goal, vector<int> p, map<string, int> &visited)
{
	visited[now] = 1;

	if(now == goal)
	{
		flag = true;
		return;
	}

	for(int i = 0; i < p.size(); i++)
	{
		swap(now, p[i], p[i]+1);
		if(visited[now] == 0)
		{
			change(now, goal, p, visited);
		}
		if(flag == true)
		{
			return;
		}
		swap(now, p[i], p[i]+1);
	}
}

int main(void)
{
	int t;
	cin >> t;

	while(t--)
	{
		int n, m;
		string arr = "0";
		string goal = "0";
		vector<int> p;
		map<string, int> visited;

		cin >> n >> m;

		for(int i = 1; i <= n; i++)
		{
			int input;
			cin >> input;

			arr += to_string(input);
			goal += to_string(input);
		}
		sort(goal.begin(), goal.end());

		for(int i = 1; i <= m; i++)
		{
			int input;
			cin >> input;

			p.push_back(input);
		}

		change(arr, goal, p, visited);

		if(flag == true)
		{
			cout << "YES\n";
			flag = false;
		}
		else
		{
			cout << "NO\n";
		}
	}

	return 0;
}


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

bool flag = false;

void swap(string &now, int a, int b)
{
	char temp = now[a];
	now[a] = now[b];
	now[b] = temp;
}

void change(string now, string goal, vector<int> p, map<string, int> &visited)
{
	visited[now] = 1;

	if(now == goal)
	{
		flag = true;
		return;
	}

	for(int i = 0; i < p.size(); i++)
	{
		swap(now, p[i], p[i]+1);
		if(visited[now] == 0)
		{
			change(now, goal, p, visited);
		}
		if(flag == true)
		{
			return;
		}
		swap(now, p[i], p[i]+1);
	}
}

int main(void)
{
	int t;
	cin >> t;

	while(t--)
	{
		int n, m;
		string arr = "0";
		string goal = "0";
		string swap;
		vector<int> p;
		map<string, int> visited;

		cin >> n >> m;

		for(int i = 1; i <= n; i++)
		{
			int input;
			cin >> input;

			arr += to_string(input);
			goal += to_string(input);
		}
		sort(goal.begin(), goal.end());
		visited[arr] = 1;

		cin.ignore();
		getline(cin, swap);

		for(int i = 0; i < swap.size(); i += 2)
		{
			p.push_back(swap[i]-'0');
		}

		change(arr, goal, p, visited);

		if(flag == true)
		{
			cout << "YES\n";
			flag = false;
		}
		else
		{
			cout << "NO\n";
		}
	}

	return 0;
}


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

bool flag = false;

void swap(string &now, int a, int b)
{
	char temp = now[a];
	now[a] = now[b];
	now[b] = temp;
}

void change(string now, string goal, vector<int> p, map<string, int> visited)
{
	if(now == goal)
	{
		flag = true;
		return;
	}

	for(int i = 0; i < p.size(); i++)
	{
		swap(now, p[i], p[i]+1);
		if(visited[now] == 0)
		{
			visited[now] = 1;
			change(now, goal, p, visited);
		}
		if(flag == true)
		{
			return;
		}
		swap(now, p[i], p[i+1]);
	}
}

int main(void)
{
	int t;
	cin >> t;

	while(t--)
	{
		int n, m;
		string arr = "0";
		string goal = "0";
		string swap;
		vector<int> p;
		map<string, int> visited;

		cin >> n >> m;

		for(int i = 1; i <= n; i++)
		{
			int input;
			cin >> input;

			arr += to_string(input);
			goal += to_string(input);
		}
		sort(goal.begin(), goal.end());
		visited[arr] = 1;

		cin.ignore();
		getline(cin, swap);

		for(int i = 0; i < swap.size(); i += 2)
		{
			p.push_back(swap[i]-'0');
		}

		if(flag == true)
		{
			cout << "YES\n";
			flag = false;
		}
		else
		{
			cout << "NO\n";
		}
	}

	return 0;
}


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

int main(void)
{
	int t;
	cin >> t;

	while(t--)
	{
		int n, m;
		cin >> n >> m;

		string s;
		string goal;
		int visited[110] = {0};

		for(int i = 1; i <= n; i++)
		{
			int input;
			cin >> input;

			s += to_string(input);
			goal += to_string(input);
		}
		sort(goal.begin(), goal.end());

		for(int i = 1; i <= m; i++)
		{
			int input;
			cin >> input;

			visited[--input] = 1;
		}

		for(int i = 0; i < n-1; i++)
		{
			int j = i;

			while(visited[j] == 1)
			{
				j++;
			}

			sort(s.begin()+i, s.begin()+j+1);

			i = j;
		}

		if(s == goal)
		{
			cout << "YES\n";
		}
		else
		{
			cout << "NO\n";
		}
	}

	return 0;
}


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

int main(void)
{
	int t;
	cin >> t;

	while(t--)
	{
		int n, m;
		cin >> n >> m;

		string s;
		string goal;
		int visited[110] = {0};

		for(int i = 1; i <= n; i++)
		{
			int input;
			cin >> input;

			s += to_string(input);
			goal += to_string(input);
		}
		sort(goal.begin(), goal.end());

		for(int i = 1; i <= m; i++)
		{
			int input;
			cin >> input;

			visited[--input] = 1;
		}

		for(int i = 0; i < n; i++)
		{
			int j = i;

			while(visited[j] == 1)
			{
				j++;
			}

			sort(s.begin()+i, s.begin()+j+1);

			i = j;
		}

		if(s == goal)
		{
			cout << "YES\n";
		}
		else
		{
			cout << "NO\n";
		}
	}

	return 0;
}


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

int main(void)
{
	int t;
	cin >> t;

	while(t--)
	{
		int n, m;
		cin >> n >> m;

		vector<int> s;
		vector<int> goal;
		int visited[110] = {0};

		for(int i = 1; i <= n; i++)
		{
			int input;
			cin >> input;

			s.push_back(input);
			goal.push_back(input);
		}
		sort(goal.begin(), goal.end());

		for(int i = 1; i <= m; i++)
		{
			int input;
			cin >> input;

			visited[--input] = 1;
		}

		for(int i = 0; i < n-1; i++)
		{
			int j = i;

			while(visited[j] == 1)
			{
				j++;
			}

			sort(s.begin()+i, s.begin()+j+1);

			i = j;
		}

		if(s == goal)
		{
			cout << "YES\n";
		}
		else
		{
			cout << "NO\n";
		}
	}

	return 0;
}


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

bool flag = false;

string swap(string now, int a, int b)
{
	char temp = now[a];
	now[a] = now[b];
	now[b] = temp;

	return now;
}

void change(string now, string goal, vector<int> p, map<string, int> &visited)
{
	visited[now] = 1;

	if(now == goal)
	{
		flag = true;
		return;
	}

	for(int i = 0; i < p.size(); i++)
	{
		string swap_now = swap(now, p[i], p[i]+1);
		if(visited[swap_now] == 0)
		{
			change(swap_now, goal, p, visited);
		}
		if(flag == true)
		{
			return;
		}
	}
}

int main(void)
{
	int t;
	cin >> t;

	while(t--)
	{
		int n, m;
		string arr = "0";
		string goal = "0";
		vector<int> p;
		map<string, int> visited;

		cin >> n >> m;

		for(int i = 1; i <= n; i++)
		{
			int input;
			cin >> input;

			arr += to_string(input);
			goal += to_string(input);
		}
		sort(goal.begin(), goal.end());

		for(int i = 1; i <= m; i++)
		{
			int input;
			cin >> input;

			p.push_back(input);
		}

		change(arr, goal, p, visited);

		if(flag == true)
		{
			cout << "YES\n";
			flag = false;
		}
		else
		{
			cout << "NO\n";
		}
	}

	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> A;
vector<int> p;
int main()
{
	ios::sync_with_stdio(false);
	int C;
	cin >> C;
	while (C--)
	{
		int n, m;
		cin >> n >> m;
		A.resize(n);
		for (int i = 0; i < n; i++)
			cin >> A[i];
		p.resize(m+1);
		for (int i = 1; i <= m; i++)
			cin >> p[i];
		sort(p.begin(), p.end());		// p정렬해줌
		p.push_back(n);
		vector<int>temp = A;
		sort(temp.begin(), temp.end());
		bool isok = true;
		for (int i = 1; i < p.size(); i++)
		{
			for (int j = p[i-1] + 1; j < p[i]; j++)
			{
				if (A[j-1] >= temp[p[i]-1])
				{
					isok = false;
					break;
				}
			}
			if (isok == false)
				break;
		}
		if (isok)
			cout << "YES" << "\n";
		else
			cout << "NO" << "\n";
	}

}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> A;
vector<int> p;
int main()
{
	ios::sync_with_stdio(false);
	int C;
	cin >> C;
	while (C--)
	{
		int n, m;
		cin >> n >> m;
		A.resize(n);
		for (int i = 0; i < n; i++)
			cin >> A[i];
		p.resize(m);
		for (int i = 0; i < m; i++)
			cin >> p[i];
		sort(p.begin(), p.end());		// p정렬해줌

		int last = p[0];
		int llast = p[0];
		for (int i = 1; i < p.size(); i++)
		{
			if (p[i] == p[i - 1] + 1)
			{
				llast = p[i];
				continue;
			}
			sort(A.begin() + last - 1, A.begin() + p[i - 1] + 1);
			last = p[i];
			llast = p[i];
		}

		sort(A.begin() + last - 1, A.begin() + llast + 1);

		bool isok = true;
		for (int i = 1; i < A.size(); i++)
		{
			if (A[i] >= A[i - 1])
				continue;
			isok = false;
			break;
		}
		if (isok)
			cout << "YES\n";
		else
			cout << "NO\n";
	}

}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> A;
vector<int> p;
int main()
{
	ios::sync_with_stdio(false);
	int C;
	cin >> C;
	while (C--)
	{
		int n, m;
		cin >> n >> m;
		A.resize(n);
		for (int i = 0; i < n; i++)
			cin >> A[i];
		p.resize(m+1);
		for (int i = 1; i <= m; i++)
			cin >> p[i];
		sort(p.begin(), p.end());		// p정렬해줌
		p.push_back(n);
		vector<int>temp = A;
		sort(temp.begin(), temp.end());
		bool isok = true;
		for (int i = 1; i < p.size(); i++)
		{
			if (p[i - 1] + 1 == p[i])
				continue;
			for (int j = 1; j < p[i]; j++)
			{
				if (A[j - 1] >= temp[p[i] - 1])
				{
					if (A[j - 1] == temp[p[i] - 2])
						continue;
					isok = false;
					break;
				}

			}
			if (isok == false)
				break;
		}
		if (isok)
			cout << "YES" << "\n";
		else
			cout << "NO" << "\n";
	}

}


#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
vector<int> A;
vector<int> p;
int n, m;
vector<int> sorted;
map<vector<int>,bool> ma;
void dfs(int idx)
{

	ma[sorted] = 1;
	for (int i = 0; i < p.size(); i++)
	{
		swap(sorted[p[i] - 1], sorted[p[i]]);
		if(ma.count(sorted) == 0)
			dfs(p[i]);
		swap(sorted[p[i] - 1], sorted[p[i]]);
	}

}
void foo()
{
	ma.clear();			// 초기화
	ma[sorted] = 1;
	for (int i = 0; i < p.size(); i++)
	{
		swap(sorted[p[i] - 1], sorted[p[i]]);
		if (ma.count(A) == 0)
			dfs(p[i]);
		swap(sorted[p[i] - 1], sorted[p[i]]);
	}
}

int main()
{
	ios::sync_with_stdio(false);
	int C;
	cin >> C;
	while (C--)
	{
		cin >> n >> m;
		A.resize(n);
		p.resize(m);
		for (int i = 0; i < n; i++)
			cin >> A[i];
		for (int i = 0; i < m; i++)
			cin >> p[i];
		sorted = A;
		sort(sorted.begin(), sorted.end());
		foo();
		if (ma.count(A))
			cout << "YES" << "\n";
		else
			cout << "NO" << "\n";
	}

}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> A;
vector<int> p;
int main()
{
	ios::sync_with_stdio(false);
	int C;
	cin >> C;
	while (C--)
	{
		int n, m;
		cin >> n >> m;
		A.resize(n);
		for (int i = 0; i < n; i++)
			cin >> A[i];
		p.resize(m+1);
		for (int i = 1; i <= m; i++)
			cin >> p[i];
		sort(p.begin(), p.end());		// p정렬해줌
		p.push_back(n);
		vector<int>temp = A;
		sort(temp.begin(), temp.end());
		bool isok = true;
		for (int i = 1; i < p.size(); i++)
		{
			if (p[i - 1] + 1 == p[i])
				continue;
			for (int j = 1; j < p[i]; j++)
			{

				if (A[j - 1] >= temp[p[i] - 1])
				{
					isok = false;
					break;
				}
			}
			if (isok == false)
				break;
		}
		if (isok)
			cout << "YES" << "\n";
		else
			cout << "NO" << "\n";
	}

}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> A;
vector<int> p;
int main()
{
	ios::sync_with_stdio(false);
	int C;
	cin >> C;
	while (C--)
	{
		int n, m;
		cin >> n >> m;
		A.resize(n);
		for (int i = 0; i < n; i++)
			cin >> A[i];
		p.resize(m+1);
		for (int i = 1; i <= m; i++)
			cin >> p[i];
		sort(p.begin(), p.end());		// p정렬해줌
		p.push_back(n);

		bool isok = true;
		for (int i = 1; i < p.size(); i++)
		{
			for (int j = p[i-1] + 1; j < p[i]; j++)
			{
				if (A[j-1] >= p[i])
				{
					isok = false;
					break;
				}
			}
			if (isok == false)
				break;
		}
		if (isok)
			cout << "YES" << "\n";
		else
			cout << "NO" << "\n";
	}

}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> A;
vector<int> p;
int main()
{
	ios::sync_with_stdio(false);
	int C;
	cin >> C;
	while (C--)
	{
		int n, m;
		cin >> n >> m;
		A.resize(n);
		for (int i = 0; i < n; i++)
			cin >> A[i];
		p.resize(m+1);
		for (int i = 1; i <= m; i++)
			cin >> p[i];
		sort(p.begin(), p.end());		// p정렬해줌
		p.push_back(n);
		vector<int>temp = A;
		sort(temp.begin(), temp.end());
		bool isok = true;
		for (int i = 1; i < p.size(); i++)
		{
			for (int j = p[i-1] + 1; j < p[i]; j++)
			{
				if (A[j-1] >= A[p[i]-1])
				{
					isok = false;
					break;
				}

			}
			if (isok == false)
				break;
		}
		if (isok)
			cout << "YES" << "\n";
		else
			cout << "NO" << "\n";
	}

}


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

struct AA{
    int s, t, k, v;
};
AA a[110];
int b[110];
bool cmp(AA a, AA b) {
    return a.v < b.v;
}

int main() {
    int t, m, n;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &m, &n);
        for (int i = 1; i <= m; i++) {
            scanf ("%d", &a[i].v);
            a[i].s = i;
        }
        sort(a + 1, a + 1 + m ,cmp);
        for (int i = 1; i <= m; i ++) {
            a[i].t = i;
            a[i].k = 0;
            //cout <<a[i].k << endl;
        }
        for(int i = 1; i <= n; i++) {
            scanf("%d", &b[i]);
            a[b[i]].k = 1;
        }
        int flag = 1;
        for (int i = 1;i <= m; i++) {
            if(a[i].s < a[i].t) {
                for (int j = a[i].s ; j < a[i].t; j++)
                if(!a[j].k) {
                    flag = 0;
                    break;
                }
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
}

/*
100
4 2
4 1 2 3
3 2
4 2
2 1 4 3
1 3
4 2
4 3 2 1
1 3
*/
/*

#include<bits/stdc++.h>
#define debug(x) cout<<#x<<'='<<x<<endl
#define set0(x) memset(x,0,sizeof(x))
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1e3+10;
int a[maxn],vis[maxn];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {	int flag=1;
    	int n,m;
    	set0(vis),set0(a);
    	cin>>n>>m;
    	for(int i=1;i<=n;i++) cin>>a[i];
    	while(m--)
    	{
    		int x;
    		cin>>x;
    		vis[x]=1;
    		if(a[x]>a[x+1]) swap(a[x],a[x+1]);
		}
		for(int i=1;i<=n-1;i++)
		{
			if(a[i]>a[i+1]&&vis[i]==0)
			{
				flag=0;
				break;
			}
		}
		if(flag) puts("YES");
		else puts("NO");
	}
}
*/


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

struct AA{
    int s, t, k, v;
};
AA a[110];
int b[110];
bool cmp(AA a, AA b) {
    return a.v < b.v;
}

int main() {
    int t, m, n;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++) {
            scanf ("%d", &a[i].v);
            a[i].s = i;
        }
        sort(a + 1, a + 1 + n,cmp);
        for (int i = 1; i <= n; i ++) {
            a[i].t = i;
            a[i].k = 0;
         }
        for(int i = 1; i <= m; i++) {
            scanf("%d", &b[i]);
            a[b[i]].k = 1;
        }
        for (int i = 1; i <= n; i++)
            cout << a[i].v <<" " << a[i].s - 1 <<" " << a[i].t<< endl;
        int flag = 1;
        for (int i = 1;i <= n; i++) {
            if(a[i].s < i) {
                for (int j = a[i].s ; j < a[i].t; j++)
                if(!a[j].k) {
                    flag = 0;
                    break;
                }
            }
            if(flag == 0) break;
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
}
/*
#include <bits/stdc++.h>

using namespace std;

int p[110];
pair<int, int> a[110];
bool mark[110];
int n, m;

void solve() {
    cin >> n >> m;
    memset(mark, false, sizeof(mark));
    for (int i = 0; i < n; i++) {
        cin >> a[i].first;
        a[i].second = i;
    }
    for (int i = 0; i < m; i++) {
        cin >> p[i];
        p[i]--;
        mark[p[i]] = true;
    }
    sort(a, a + n);
    for (int i = 0; i < n; i++)
        cout << a[i].first <<" " << a[i].second <<endl;
    for (int i = 0; i < n; i++) {
        if (a[i].second < i) {//i���ź���ı��룬���ԭ���ı���<i����ģ��ð������
            for (int j = a[i].second; j < i; j++) {
                if (!mark[j]) {
                    cout << "NO\n";
                    return;
                }
            }
        }
    }
    cout << "YES\n";
}

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

    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}

*/


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

struct AA{
    int s, t, k, v;
};
AA a[110];
int b[110];
bool cmp(AA a, AA b) {
    if(a.v != b.v)
    return a.v < b.v;
    else return a.s < b.s;
}

int main() {
    int t, m, n;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++) {
            scanf ("%d", &a[i].v);
            a[i].s = i;
        }
        sort(a + 1, a + 1 + n,cmp);
        for (int i = 1; i <= n; i ++) {
            a[i].t = i;
            a[i].k = 0;
         }
        for(int i = 1; i <= m; i++) {
            scanf("%d", &b[i]);
            a[b[i]].k = 1;
        }
        int flag = 1;
        for (int i = 1;i <= n; i++) {
            if(a[i].s < i) {
                for (int j = a[i].s ; j < a[i].t; j++)
                if(!a[j].k) {
                    flag = 0;
                    break;
                }
            }
            if(flag == 0) break;
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
}


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

struct AA{
    int s, t, k, v;
};
AA a[102];
int b[102];
bool cmp(AA a, AA b) {
    return a.v < b.v;
}

int main() {
    int t, m, n;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &m, &n);
        for (int i = 1; i <= m; i++) {
            scanf ("%d", &a[i].v);
            a[i].s = i;
        }
        for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
        sort(a + 1, a + 1 + m ,cmp);
        for (int i = 1; i <= m; i ++) {
            a[i].t = i;
            a[i].k = 0;
            //cout <<a[i].k << endl;
        }
        for(int i = 1; i <= n; i++) {
            a[b[i]].k = 1;
        }
        int flag = 1;
        for (int i = 1;i <= n; i++) {
            if(a[i].t < a[i].s) {
                for (int j = a[i].t ; j < a[i].s; j++ )
                if(!a[j].k) {
                    flag = 0;
                    break;
                }
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
}

/*
100
4 2
4 1 2 3
3 2
4 2
2 1 4 3
1 3
4 2
4 3 2 1
1 3
*/
/*

#include<bits/stdc++.h>
#define debug(x) cout<<#x<<'='<<x<<endl
#define set0(x) memset(x,0,sizeof(x))
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1e3+10;
int a[maxn],vis[maxn];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {	int flag=1;
    	int n,m;
    	set0(vis),set0(a);
    	cin>>n>>m;
    	for(int i=1;i<=n;i++) cin>>a[i];
    	while(m--)
    	{
    		int x;
    		cin>>x;
    		vis[x]=1;
    		if(a[x]>a[x+1]) swap(a[x],a[x+1]);
		}
		for(int i=1;i<=n-1;i++)
		{
			if(a[i]>a[i+1]&&vis[i]==0)
			{
				flag=0;
				break;
			}
		}
		if(flag) puts("YES");
		else puts("NO");
	}
}
*/


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

struct AA{
    int s, t, k, v;
};
AA a[110];
int b[110];
bool cmp(AA a, AA b) {
    return a.v < b.v;
}

int main() {
    int t, m, n;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &m, &n);
        for (int i = 1; i <= m; i++) {
            scanf ("%d", &a[i].v);
            a[i].s = i;
        }
        sort(a + 1, a + 1 + m ,cmp);
        for (int i = 1; i <= m; i ++) {
            a[i].k = 0;
        }
        for(int i = 1; i <= n; i++) {
            scanf("%d", &b[i]);
            a[b[i]].k = 1;
        }
        int flag = 1;
        for (int i = 1;i <= m; i++) {
            if(a[i].s < i) {
                for (int j = a[i].s ; j < i; j++)
                if(!a[j].k) {
                    flag = 0;
                    break;
                }
            }
            if(flag == 0) break;
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
}


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

struct AA{
    int s, t, k, v;
};
AA a[102];
int b[102];
bool cmp(AA a, AA b) {
    return a.v < b.v;
}

int main() {
    int t, m, n;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &m, &n);
        for (int i = 1; i <= m; i++) {
            scanf ("%d", &a[i].v);
            a[i].s = i;
        }
        for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
        sort(a + 1, a + 1 + m ,cmp);
        for (int i = 1; i <= m; i ++) {
            a[i].t = i;
            a[i].k = 0;
            //cout <<a[i].k << endl;
        }
        for(int i = 1; i <= n; i++) {
            a[b[i]].k = 1;
        }
        int flag = 1;
        for (int i = 1;i <= m; i++) {
            if(a[i].s < a[i].t) {
                for (int j = a[i].s ; j < a[i].t; j++)
                if(!a[j].k) {
                    flag = 0;
                    break;
                }
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
}

/*
100
4 2
4 1 2 3
3 2
4 2
2 1 4 3
1 3
4 2
4 3 2 1
1 3
*/
/*

#include<bits/stdc++.h>
#define debug(x) cout<<#x<<'='<<x<<endl
#define set0(x) memset(x,0,sizeof(x))
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1e3+10;
int a[maxn],vis[maxn];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {	int flag=1;
    	int n,m;
    	set0(vis),set0(a);
    	cin>>n>>m;
    	for(int i=1;i<=n;i++) cin>>a[i];
    	while(m--)
    	{
    		int x;
    		cin>>x;
    		vis[x]=1;
    		if(a[x]>a[x+1]) swap(a[x],a[x+1]);
		}
		for(int i=1;i<=n-1;i++)
		{
			if(a[i]>a[i+1]&&vis[i]==0)
			{
				flag=0;
				break;
			}
		}
		if(flag) puts("YES");
		else puts("NO");
	}
}
*/


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

struct AA{
    int s, t, k, v;
};
AA a[110];
int b[110];
bool cmp(AA a, AA b) {
    return a.v < b.v;
}

int main() {
    int t, m, n;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &m, &n);
        for (int i = 1; i <= m; i++) {
            scanf ("%d", &a[i].v);
            a[i].s = i;
        }
        sort(a + 1, a + 1 + m ,cmp);
        for (int i = 1; i <= m; i ++) {
            a[i].t = i;
            a[i].k = 0;
            //cout <<a[i].k << endl;
        }
        for(int i = 1; i <= n; i++) {
            scanf("%d", &b[i]);
            a[b[i]].k = 1;
        }
        int flag = 1;
        for (int i = 1;i <= m; i++) {
            if(a[i].s < a[i].t) {
                for (int j = a[i].s ; j < a[i].t; j++)
                if(!a[j].k) {
                    flag = 0;
                    break;
                }
            }
            if(flag == 0) break;
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
}


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

struct AA{
    int s, t, k, v;
};
AA a[110];
int b[110];
bool cmp(AA a, AA b) {
    return a.v < b.v;
}

int main() {
    int t, m, n;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++) {
            scanf ("%d", &a[i].v);
            a[i].s = i;
        }
        sort(a + 1, a + 1 + n,cmp);
        for (int i = 1; i <= n; i ++) {
            a[i].t = i;
            a[i].k = 0;
         }
        for(int i = 1; i <= m; i++) {
            scanf("%d", &b[i]);
            a[b[i]].k = 1;
        }
        int flag = 1;
        for (int i = 1;i <= n; i++) {
            if(a[i].s < i) {
                for (int j = a[i].s ; j < a[i].t; j++)
                if(!a[j].k) {
                    flag = 0;
                    break;
                }
            }
            if(flag == 0) break;
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
}


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

#define eps 1e-8
#define vi vector<int>
#define pb(x) push_back(x)
#define f(i,x,y) for(int i=x;i<y;i++)
#define rf(i,y,x) for(int i=y;i>=x;i--)
#define all(x) (x).begin(),(x).end()
#define cout1(x) cout<<x<<endl
#define cout2(x,y) cout<<x<<" "<<y<<endl
#define cout3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl
#define bit(x) __builtin_pocount(x)
#define MAXN 100005
//^
using namespace std;

struct cosa{
    int p, label;
    cosa(int _p, int _label){p=_p;label=_label;}
    cosa(){}
}A[MAXN];

bool operator<(cosa C1, cosa C2){
    return C1.p < C2.p;
}

vector<int>V;
int v;

int process(priority_queue<cosa> &Q,int x){
    int ans = 0;
    while(v>=x){
        if(Q.size()==0)break;
        cosa C = Q.top();
        Q.pop();
        V.pb(C.label);
        v -=x;
    }
    return ans;
}

int P[MAXN];
int main(){

    int n;
    while(cin>>n>>v){

        int x, y;
        priority_queue<cosa>Q[3];
        for(int i=0;i<n;i++){
            cin>>x>>y;
            P[i]=y;
            Q[x].push(cosa(y,i));
        }

        int ans = 0;

        while( Q[1].size() != 0 || Q[2].size() != 0 ){

            if(Q[1].size() == 0){process(Q[2], 2);break;}
            if(Q[2].size() == 0){process(Q[1], 1);break;}
            if( v == 0 )break;

            if(Q[1].size()>1){
                cosa C1 = Q[1].top(); Q[1].pop();
                cosa C2 = Q[1].top(); Q[1].pop();
                cosa C3 = Q[2].top(); Q[2].pop();

                if(C3.p <= C1.p)V.pb(C1.label), Q[2].push(C3), Q[1].push(C2), v--;
                else{
                    if(v == 1){
                        V.pb(C1.label), Q[2].push(C3), Q[1].push(C2);
                        v --;
                    }
                    else{
                        if(C3.p < C2.p + C1.p)V.pb(C1.label), V.pb(C2.label), Q[2].push(C3);
                        else V.pb(C3.label), Q[1].push(C1), Q[1].push(C2);
                        v -= 2;
                    }
                }
            }
            else{
                cosa C1 = Q[1].top(); Q[1].pop();
                cosa C3 = Q[2].top(); Q[2].pop();
                if(C3.p <= C1.p)V.pb(C1.label), Q[2].push(C3), v--;
                else{
                    if(v > 1)V.pb(C3.label), Q[1].push(C1), v -= 2;
                    else V.pb(C1.label), Q[2].push(C3), v--;
                }
            }
        }

        for(int i=0;i<V.size();i++)ans += P[V[i]];

        printf("%d",ans);puts("");
        for(int i=0;i<V.size();i++){
            if(i == 0)printf("%d",V[i]+1);
            else printf(" %d",V[i]+1);
        }
        puts("");

    }
}


#include<iostream>
#include<algorithm>
#include<vector>
#define MAXN 100005

using namespace std;
struct cosa{
    int t,p,label;
        double w;
        cosa(int _t,int _p,int _label,double _w){t=_t; p=_p;label=_label;w=_w;}
        cosa(){}
}T[MAXN];

bool operator <(cosa c1, cosa c2){
        if(c1.w != c2.w)return c1.w>c2.w;
        return c1.p > c2.p;
}
int main(){
        int n,v;
        while(cin>>n>>v){
                int x,y;
                for(int i=0;i<n;i++){
                        cin>>x>>y;
                        T[i] = cosa(x,y,i,(y*1.0)/(1.0*x));
                }

                sort(T,T+n);
                int ans = 0;
                vector<int>V;
                int last = 0;
                for(int i=0;i<n;i++)if(T[i].t == 1)last = i;
                for(int i=0;i<n;i++){
                        if(i==last)continue;
                        if(v>=T[i].t){
                                v-=T[i].t;
                                ans += T[i].p;
                                V.push_back(T[i].label);
                        }
                }
                cout<<ans<<endl;
                for(int i=0;i<V.size();i++){
                        if(i == 0)cout<<V[i]+1;
                        else cout<<" "<<V[i+1];
                }
                cout<<endl;

        }
}


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

#define eps 1e-8
#define vi vector<int>
#define pb(x) push_back(x)
#define f(i,x,y) for(int i=x;i<y;i++)
#define rf(i,y,x) for(int i=y;i>=x;i--)
#define all(x) (x).begin(),(x).end()
#define cout1(x) cout<<x<<endl
#define cout2(x,y) cout<<x<<" "<<y<<endl
#define cout3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl
#define bit(x) __builtin_pocount(x)
#define MAXN 5005
//^
using namespace std;

struct cosa{
    int w, p;
    cosa(int _w,int _p){w=_w; p=_p;}
    cosa(){}
}A[MAXN];
int p[MAXN];

bool comp(int x, int y){
    if(A[x].p != A[y].p)return A[x].p > A[y].p;
    return A[x].w < A[y].w;
}

int main(){

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

        int x, y;
        for(int i=0;i<n;i++){
            cin>>x>>y;
            A[i]=cosa(x, y);
            p[i] = i;
        }

        sort(p, p+n,comp);
        int ans = 0;
        vector<int>V;

        for(int i = 0;i < n;i++){
            if(A[ p[i] ].w <= v){
                ans += A[ p[i] ].p;
                v -= A[ p[i] ].w;
                V.pb(p[i]+1);
            }
        }

        printf("%d",ans);puts("");
        for(int i=0;i<V.size();i++){
            if(i == 0)printf("%d",V[i]);
            else printf(" %d",V[i]);
        }
        puts("");

    }
}


#include<iostream>
#include<algorithm>
#include<vector>
#define MAXN 100005

using namespace std;
struct cosa{
    int t,p,label;
        double w;
        cosa(int _t,int _p,int _label,double _w){t=_t; p=_p;label=_label;w=_w;}
        cosa(){}
}T[MAXN];

bool operator <(cosa c1, cosa c2){
        if(c1.w != c2.w)return c1.w>c2.w;
        return c1.p > c2.p;
}
int main(){
        int n,v;
        while(cin>>n>>v){
                int x,y;
                for(int i=0;i<n;i++){
                        cin>>x>>y;
                        T[i] = cosa(x,y,i,(y*1.0)/(1.0*x));
                }

                sort(T,T+n);
                int ans = 0;
                vector<int>V;

                for(int i=0;i<n;i++){
                        if(v>=T[i].t){
                                v-=T[i].t;
                                ans += T[i].p;
                                V.push_back(T[i].label);
                        }
                }
                cout<<ans<<endl;
                for(int i=0;i<V.size();i++){
                        if(i == 0)cout<<V[i]+1;
                        else cout<<" "<<V[i]+1;
                }
                cout<<endl;

        }
}


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

#define eps 1e-8
#define vi vector<int>
#define pb(x) push_back(x)
#define f(i,x,y) for(int i=x;i<y;i++)
#define rf(i,y,x) for(int i=y;i>=x;i--)
#define all(x) (x).begin(),(x).end()
#define cout1(x) cout<<x<<endl
#define cout2(x,y) cout<<x<<" "<<y<<endl
#define cout3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl
#define bit(x) __builtin_pocount(x)
#define MAXN 100005
//^
using namespace std;

struct cosa{
    int p, label;
    cosa(int _p, int _label){p=_p;label=_label;}
    cosa(){}
}A[MAXN];

bool operator<(cosa C1, cosa C2){
    return C1.p < C2.p;
}

vector<int>V;
int v;

int process(priority_queue<cosa> &Q,int x){
    int ans = 0;
    while(v>=x){
        if(Q.size()==0)break;
        cosa C = Q.top();
        Q.pop();
        V.pb(C.label);
        v -=x;
    }
    return ans;
}

int P[MAXN];
int main(){

    int n;
    while(cin>>n>>v){

        int x, y;
        priority_queue<cosa>Q[3];
        for(int i=0;i<n;i++){
            cin>>x>>y;
            P[i]=y;
            Q[x].push(cosa(y,i));
        }

        int ans = 0;

        while(!Q[1].empty() || !Q[2].empty() ){

            if(Q[1].size() == 0){process(Q[2], 2);break;}
            if(Q[2].size() == 0){process(Q[1], 1);break;}
            if( v == 0 )break;

            if(Q[1].size()>1){
                cosa C1 = Q[1].top(); Q[1].pop();
                cosa C2 = Q[1].top(); Q[1].pop();
                cosa C3 = Q[2].top(); Q[2].pop();

                if(C3.p < C1.p)V.pb(C1.label), Q[2].push(C3), Q[1].push(C2), v--;
                else{
                    if(v < 2)break;
                    if(C3.p < C2.p + C1.p)V.pb(C1.label), V.pb(C2.label), Q[2].push(C3);
                    else V.pb(C3.label), Q[1].push(C1), Q[1].push(C2);
                    v -= 2;
                }
            }
            else{
                cosa C1 = Q[1].top(); Q[1].pop();
                cosa C3 = Q[2].top(); Q[2].pop();
                if(C3.p < C1.p)V.pb(C1.label), Q[2].push(C3), v--;
                else{
                    if(v > 1)V.pb(C3.label), Q[1].push(C1), v -= 2;
                    else break;
                }
            }
        }

        for(int i=0;i<V.size();i++)ans += P[V[i]];

        printf("%d",ans);puts("");
        for(int i=0;i<V.size();i++){
            if(i == 0)printf("%d",V[i]+1);
            else printf(" %d",V[i]+1);
        }
        puts("");

    }
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cstring>
#define MAXN 100005

using namespace std;
struct cosa{
    int t,p,label;
        double w;
        cosa(int _t,int _p,int _label,double _w){t=_t; p=_p;label=_label;w=_w;}
        cosa(){}
}T[MAXN];

bool operator <(cosa c1, cosa c2){
        if(c1.w != c2.w)return c1.w>c2.w;
        if(c1.p != c2.p)return c1.p > c2.p;
        return c1.t>c2.t;
}
int main(){
        int n,v;
        while(cin>>n>>v){
                int x,y;
                for(int i=0;i<n;i++){
                        cin>>x>>y;
                        T[i] = cosa(x,y,i,(y*1.0)/(1.0*x));
                }

                sort(T,T+n);
                //for(int i=0;i<n;i++)cout<<T[i].t<<" "<<T[i].p<<endl;
                int ans = 0;
                vector<int>V;
                int last = -1, pos=-1;
                for(int i=0;i<n;i++){
                        if(v>=T[i].t){
                                v-=T[i].t;
                                ans += T[i].p;
                                V.push_back(T[i].label);
                                if(T[i].t == 1)pos = i;
                                last=i;
                        }
                }
        //cout<<v<<endl;puts("MIRAAA");
        if(v==1){
                if(pos == -1 || last == -1){
                    cout<<0<<endl;
                    continue;
                }
                //cout<<T[pos].t<<" "<<T[pos].p<<endl;
                v ++;
                ans -= T[pos].p;
                ans += T[last+1].p;
                V.push_back(T[last+1].label);

            cout<<ans<<endl;
                bool ok = 0;
                for(int i=0;i<V.size();i++){
                        if(V[i] == T[pos].label)continue;
                        if(!ok)cout<<V[i]+1;
                        else cout<<" "<<V[i]+1;
                        ok=1;
                }
                cout<<endl;
        }
        else{
            cout<<ans<<endl;
                for(int i=0;i<V.size();i++){
                        if(i==0)cout<<V[i]+1;
                        else cout<<" "<<V[i]+1;
                }
                cout<<endl;

        }

        }
}


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

#define eps 1e-8
#define vi vector<int>
#define pb(x) push_back(x)
#define f(i,x,y) for(int i=x;i<y;i++)
#define rf(i,y,x) for(int i=y;i>=x;i--)
#define all(x) (x).begin(),(x).end()
#define cout1(x) cout<<x<<endl
#define cout2(x,y) cout<<x<<" "<<y<<endl
#define cout3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl
#define bit(x) __builtin_pocount(x)
#define MAXN 100005
//^
using namespace std;

struct cosa{
    int p, label;
    cosa(int _p, int _label){p=_p;label=_label;}
    cosa(){}
}A[MAXN];

bool operator<(cosa C1, cosa C2){
    return C1.p < C2.p;
}

vector<int>V;
int v;

int process(priority_queue<cosa> &Q,int x){
    int ans = 0;
    while(v>=x){
        if(Q.size()==0)break;
        cosa C = Q.top();
        Q.pop();
        V.pb(C.label);
        v -=x;
    }
    return ans;
}

int P[MAXN];
int main(){

    int n;
    while(cin>>n>>v){

        int x, y;
        priority_queue<cosa>Q[3];
        for(int i=0;i<n;i++){
            cin>>x>>y;
            P[i]=y;
            Q[x].push(cosa(y,i));
        }

        int ans = 0;

        while( Q[1].size() != 0 || Q[2].size() != 0 ){

            if(Q[1].size() == 0){process(Q[2], 2);break;}
            if(Q[2].size() == 0){process(Q[1], 1);break;}
            if( v == 0 )break;

            if(Q[1].size()>1){
                cosa C1 = Q[1].top(); Q[1].pop();
                cosa C2 = Q[1].top(); Q[1].pop();
                cosa C3 = Q[2].top(); Q[2].pop();

                if(C3.p <= C1.p)V.pb(C1.label), Q[2].push(C3), Q[1].push(C2), v--;
                else{
                    if(v == 1){
                        if(C3.p <= C1.p)V.pb(C1.label), Q[2].push(C3), Q[1].push(C2);
                        else V.pb(C3.label), Q[1].push(C1), Q[1].push(C2);
                        v --;
                    }
                    else{
                        if(C3.p <= C2.p + C1.p)V.pb(C1.label), V.pb(C2.label), Q[2].push(C3);
                        else V.pb(C3.label), Q[1].push(C1), Q[1].push(C2);
                        v -= 2;
                    }
                }
            }
            else{
                cosa C1 = Q[1].top(); Q[1].pop();
                cosa C3 = Q[2].top(); Q[2].pop();
                if(C3.p <= C1.p)V.pb(C1.label), Q[2].push(C3), v--;
                else{
                    if(v > 1)V.pb(C3.label), Q[1].push(C1), v -= 2;
                    else V.pb(C1.label), Q[2].push(C3), v--;
                }
            }
        }

        for(int i=0;i<V.size();i++)ans += P[V[i]];

        printf("%d",ans);puts("");
        for(int i=0;i<V.size();i++){
            if(i == 0)printf("%d",V[i]+1);
            else printf(" %d",V[i]+1);
        }
        puts("");

    }
}


#include<iostream>
#include<algorithm>
#include<vector>
#define MAXN 100005

using namespace std;
struct cosa{
    int t,p,label;
        double w;
        cosa(int _t,int _p,int _label,double _w){t=_t; p=_p;label=_label;w=_w;}
        cosa(){}
}T[MAXN];

bool operator <(cosa c1, cosa c2){
        if(c1.w != c2.w)return c1.w>c2.w;
        return c1.p > c2.p;
}
int main(){
        int n,v;
        while(cin>>n>>v){
                int x,y;
                for(int i=0;i<n;i++){
                        cin>>x>>y;
                        T[i] = cosa(x,y,i,(y*1.0)/(1.0*x));
                }

                sort(T,T+n);
                int ans = 0;
                vector<int>V;
                int last = 0;
                for(int i=0;i<n;i++)if(T[i].t == 1)last = i;
                for(int i=0;i<n;i++){
                        if(i==last)continue;
                        if(v>=T[i].t){
                                v-=T[i].t;
                                ans += T[i].p;
                                V.push_back(T[i].label);
                        }
                        if(T[i].t == 1)last = i;
                }
                cout<<ans<<endl;
                for(int i=0;i<V.size();i++){
                        if(i == 0)cout<<V[i]+1;
                        else cout<<" "<<V[i+1];
                }
                cout<<endl;

        }
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cstring>
#define MAXN 100005

using namespace std;
struct cosa{
    int t,p,label;
        double w;
        cosa(int _t,int _p,int _label,double _w){t=_t; p=_p;label=_label;w=_w;}
        cosa(){}
}T[MAXN];

bool operator <(cosa c1, cosa c2){
        if(c1.w != c2.w)return c1.w>c2.w;
        return c1.p > c2.p;
}
int main(){
        int n,v;
        while(cin>>n>>v){
                int x,y;
                for(int i=0;i<n;i++){
                        cin>>x>>y;
                        T[i] = cosa(x,y,i,(y*1.0)/(1.0*x));
                }

                sort(T,T+n);
                //for(int i=0;i<n;i++)cout<<T[i].t<<" "<<T[i].p<<endl;
                int ans = 0;
                vector<int>V;
                int last = 0, pos=0;
                for(int i=0;i<n;i++){
                        if(v>=T[i].t){
                                v-=T[i].t;
                                ans += T[i].p;
                                V.push_back(T[i].label);
                                if(T[i].t == 1)pos = i;
                                last=i;
                        }
                }
        //cout<<v<<endl;puts("MIRAAA");
        if(v!=0){

                //cout<<T[pos].t<<" "<<T[pos].p<<endl;
                v += T[pos].t;
                ans -= T[pos].p;

                for(int i=last+1;i<n;i++){
                        if(v>=T[i].t){
                                v-=T[i].t;
                                ans += T[i].p;
                                V.push_back(T[i].label);
                        }
                }
            cout<<ans<<endl;
                bool ok = 0;
                for(int i=0;i<V.size();i++){
                        if(V[i] == T[pos].label)continue;
                        if(!ok)cout<<V[i]+1;
                        else cout<<" "<<V[i]+1;
                        ok=1;
                }
                cout<<endl;
        }
        else{
            cout<<ans<<endl;
                for(int i=0;i<V.size();i++){
                        if(i==0)cout<<V[i]+1;
                        else cout<<" "<<V[i]+1;
                }
                cout<<endl;

        }

        }
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cstring>
#define MAXN 100005

using namespace std;
struct cosa{
    int t,p,label;
        double w;
        cosa(int _t,int _p,int _label,double _w){t=_t; p=_p;label=_label;w=_w;}
        cosa(){}
}T[MAXN];

bool operator <(cosa c1, cosa c2){
        if(c1.w != c2.w)return c1.w>c2.w;
        if(c1.p != c2.p)return c1.p > c2.p;
        return c1.t>c2.t;
}
int main(){
        int n,v;
        while(cin>>n>>v){
                int x,y;
                for(int i=0;i<n;i++){
                        cin>>x>>y;
                        T[i] = cosa(x,y,i,(y*1.0)/(1.0*x));
                }

                sort(T,T+n);
                //for(int i=0;i<n;i++)cout<<T[i].t<<" "<<T[i].p<<endl;
                int ans = 0;
                vector<int>V;
                int last = 0, pos=0;
                for(int i=0;i<n;i++){
                        if(v>=T[i].t){
                                v-=T[i].t;
                                ans += T[i].p;
                                V.push_back(T[i].label);
                                if(T[i].t == 1)pos = i;
                                last=i;
                        }
                }
        //cout<<v<<endl;puts("MIRAAA");
        if(v==1){

                //cout<<T[pos].t<<" "<<T[pos].p<<endl;
                v ++;
                ans -= T[pos].p;
                ans += T[last+1].p;
                V.push_back(T[last+1].label);

            cout<<ans<<endl;
                bool ok = 0;
                for(int i=0;i<V.size();i++){
                        if(V[i] == T[pos].label)continue;
                        if(!ok)cout<<V[i]+1;
                        else cout<<" "<<V[i]+1;
                        ok=1;
                }
                cout<<endl;
        }
        else{
            cout<<ans<<endl;
                for(int i=0;i<V.size();i++){
                        if(i==0)cout<<V[i]+1;
                        else cout<<" "<<V[i]+1;
                }
                cout<<endl;

        }

        }
}


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

#define eps 1e-8
#define vi vector<int>
#define pb(x) push_back(x)
#define f(i,x,y) for(int i=x;i<y;i++)
#define rf(i,y,x) for(int i=y;i>=x;i--)
#define all(x) (x).begin(),(x).end()
#define cout1(x) cout<<x<<endl
#define cout2(x,y) cout<<x<<" "<<y<<endl
#define cout3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl
#define bit(x) __builtin_pocount(x)
#define MAXN 100005
//^
using namespace std;

struct cosa{
    int p, label;
    cosa(int _p, int _label){p=_p;label=_label;}
    cosa(){}
}A[MAXN];

bool operator<(cosa C1, cosa C2){
    return C1.p < C2.p;
}

vector<int>V;
int v;

int process(priority_queue<cosa> &Q,int x){
    int ans = 0;
    while(v>=x){
        if(Q.size()==0)break;
        cosa C = Q.top();
        Q.pop();
        V.pb(C.label);
        v -=x;
    }
    return ans;
}

int P[MAXN];
int main(){

    int n;
    while(cin>>n>>v){

        int x, y;
        priority_queue<cosa>Q[3];
        for(int i=0;i<n;i++){
            cin>>x>>y;
            P[i]=y;
            Q[x].push(cosa(y,i));
        }

        int ans = 0;

        while(!Q[1].empty() || !Q[2].empty() ){

            if(Q[1].size() == 0){process(Q[2], 2);break;}
            if(Q[2].size() == 0){process(Q[1], 1);break;}
            if( v == 0 )break;

            if(Q[1].size()>1){
                cosa C1 = Q[1].top(); Q[1].pop();
                cosa C2 = Q[1].top(); Q[1].pop();
                cosa C3 = Q[2].top(); Q[2].pop();

                if(C3.p < C1.p)V.pb(C1.label), Q[2].push(C3), Q[1].push(C2), v--;
                else{
                    if(v == 1){
                        if(C3.p < C1.p)V.pb(C1.label), Q[2].push(C3), Q[1].push(C2);
                        else V.pb(C3.label), Q[1].push(C1), Q[1].push(C2);
                        v --;
                    }
                    else{
                        if(C3.p < C2.p + C1.p)V.pb(C1.label), V.pb(C2.label), Q[2].push(C3);
                        else V.pb(C3.label), Q[1].push(C1), Q[1].push(C2);
                        v -= 2;
                    }
                }
            }
            else{
                cosa C1 = Q[1].top(); Q[1].pop();
                cosa C3 = Q[2].top(); Q[2].pop();
                if(C3.p < C1.p)V.pb(C1.label), Q[2].push(C3), v--;
                else{
                    if(v > 1)V.pb(C3.label), Q[1].push(C1), v -= 2;
                    else V.pb(C1.label), Q[2].push(C3), v--;
                }
            }
        }

        for(int i=0;i<V.size();i++)ans += P[V[i]];

        printf("%d",ans);puts("");
        for(int i=0;i<V.size();i++){
            if(i == 0)printf("%d",V[i]+1);
            else printf(" %d",V[i]+1);
        }
        puts("");

    }
}


#include <iostream>
#include <sstream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <stdio.h>

using namespace std;

#define fo(i,n) for(int i=0; i<(int)n; i++)
#define rep(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
#define mp(a,b) make_pair(a,b)
#define pb(x) push_back(x)
#define pii pair<int,int>

int n,v;
priority_queue<pair<int,int> > q1,q2;

int main() {

    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n>>v;
    for (int i=0; i<n; i++) {
        int t,p;
        scanf("%d%d",&t,&p);
        if (t==1) {
            q1.push(make_pair(p,i));
        }
        else {
            q2.push(make_pair(p,i));
        }
    }

    int tot = 0;
    vector<int> w;
    while (v>0) {
        if (q1.size()>1 && q2.size()>0) {
            if (q1.size()==2 && v%2==1) {
                pair<int,int> p1 = q1.top();
                q1.pop();
                v--;
                tot += p1.first;
                w.push_back(p1.second+1);
            }
            else {
                pair<int,int> p1 = q1.top();
                q1.pop();
                int k1 = p1.first + q1.top().first;
                int k2 = q2.top().first;
                if (k1>k2) {
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    p1 = q1.top();
                    q1.pop();
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    v -= 2;
                }
                else {
                    q1.push(p1);
                    p1 = q2.top();
                    q2.pop();
                    v -= 2;
                    tot += p1.first;
                    w.push_back(p1.second+1);
                }
            }
        }
        else {
            //cout<<v<<" "<<q1.size()<<" "<<q2.size()<<endl;
            if (q1.size()==1) {
                if (v%2==1) {
                    pair<int,int> p1 = q1.top();
                    q1.pop();
                    v--;
                    tot += p1.first;
                    w.push_back(p1.second+1);
                }
                else {
                    pair<int,int> p1 = q1.top();
                    q1.pop();
                    q2.push(p1);
                }
            }
            else if (q2.size()==0) {
                if (v==0) break;
                if (q1.size()==0) break;
                pair<int,int> p1 = q1.top();
                q1.pop();
                v--;
                tot += p1.first;
                w.push_back(p1.second+1);
            }
            else {
                if (v==1) break;
                if (q2.size()==0) break;
                pair<int,int> p1 = q2.top();
                q2.pop();
                v-=2;
                tot += p1.first;
                w.push_back(p1.second+1);
            }
        }
    }

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

    return 0;

}


#include <iostream>
#include <sstream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <stdio.h>

using namespace std;

#define fo(i,n) for(int i=0; i<(int)n; i++)
#define rep(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
#define mp(a,b) make_pair(a,b)
#define pb(x) push_back(x)
#define pii pair<int,int>

int n,v;
priority_queue<pair<int,int> > q1,q2;

int main() {

    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n>>v;
    for (int i=0; i<n; i++) {
        int t,p;
        scanf("%d%d",&t,&p);
        if (t==1) {
            q1.push(make_pair(p,i));
        }
        else {
            q2.push(make_pair(p,i));
        }
    }

    int tot = 0;
    vector<int> w;
    while (v>0) {
        if (q1.size()>1 && q2.size()>0) {
            if (q1.size()==2 && v%2==1) {
                pair<int,int> p1 = q1.top();
                q1.pop();
                tot += p1.first;
                w.push_back(p1.second+1);
            }
            else {
                pair<int,int> p1 = q1.top();
                q1.pop();
                int k1 = p1.first + q1.top().first;
                int k2 = q2.top().first;
                if (k1>k2) {
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    p1 = q1.top();
                    q1.pop();
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    v -= 2;
                }
                else {
                    q1.push(p1);
                    p1 = q2.top();
                    q2.pop();
                    v -= 2;
                    tot += p1.first;
                    w.push_back(p1.second+1);
                }
            }
        }
        else {
            //cout<<v<<" "<<q1.size()<<" "<<q2.size()<<endl;
            if (q1.size()==1) {
                if (v%2==1) {
                    pair<int,int> p1 = q1.top();
                    q1.pop();
                    v--;
                    tot += p1.first;
                    w.push_back(p1.second+1);
                }
                else {
                    pair<int,int> p1 = q1.top();
                    q1.pop();
                    q2.push(p1);
                }
            }
            else if (q2.size()==0) {
                if (v==0) break;
                if (q1.size()==0) break;
                pair<int,int> p1 = q1.top();
                q1.pop();
                v--;
                tot += p1.first;
                w.push_back(p1.second+1);
            }
            else {
                if (v==1) break;
                if (q2.size()==0) break;
                pair<int,int> p1 = q2.top();
                q2.pop();
                v-=2;
                tot += p1.first;
                w.push_back(p1.second+1);
            }
        }
    }

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

    return 0;

}


#include <iostream>
#include <sstream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <stdio.h>

using namespace std;

#define fo(i,n) for(int i=0; i<(int)n; i++)
#define rep(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
#define mp(a,b) make_pair(a,b)
#define pb(x) push_back(x)
#define pii pair<int,int>

int n,v;
priority_queue<pair<int,int> > q1,q2;

int main() {

    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n>>v;
    for (int i=0; i<n; i++) {
        int t,p;
        scanf("%d%d",&t,&p);
        if (t==1) {
            q1.push(make_pair(p,i));
        }
        else {
            q2.push(make_pair(p,i));
        }
    }

    int tot = 0;
    vector<int> w;
    while (v>0) {
        if (q1.size()>1) {
            pair<int,int> p1 = q1.top();
            q1.pop();
            int k1 = p1.first + q1.top().first;
            int k2 = q2.top().first;
            if (k1>k2) {
                tot += p1.first;
                w.push_back(p1.second+1);
                p1 = q1.top();
                q1.pop();
                tot += p1.first;
                w.push_back(p1.second+1);
                v -= 2;
            }
            else {
                q1.push(p1);
                p1 = q2.top();
                q2.pop();
                v -= 2;
                tot += p1.first;
                w.push_back(p1.second+1);
            }
        }
        else {
            if (q1.size()==1) {
                if (v%2==1) {
                    pair<int,int> p1 = q1.top();
                    q1.pop();
                    v--;
                    tot += p1.first;
                w.push_back(p1.second+1);
                }
                else {
                    pair<int,int> p1 = q1.top();
                    q1.pop();
                    q2.push(p1);
                }
            }
            else {
                if (v==1) break;
                if (q2.size()==0) break;
                pair<int,int> p1 = q2.top();
                q2.pop();
                v-=2;
                tot += p1.first;
                w.push_back(p1.second+1);
            }
        }
    }

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

    return 0;

}


#include <iostream>
#include <sstream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <stdio.h>

using namespace std;

#define fo(i,n) for(int i=0; i<(int)n; i++)
#define rep(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
#define mp(a,b) make_pair(a,b)
#define pb(x) push_back(x)
#define pii pair<int,int>

int n,v;
priority_queue<pair<int,int> > q1,q2;

int main() {

    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n>>v;
    for (int i=0; i<n; i++) {
        int t,p;
        scanf("%d%d",&t,&p);
        if (t==1) {
            q1.push(make_pair(p,i));
        }
        else {
            q2.push(make_pair(p,i));
        }
    }

    int tot = 0;
    vector<int> w;
    while (v>0) {
        if (q1.size()>1 && q2.size()>0 && v>1) {
            if (q1.size()==2 && v%2==1) {
                pair<int,int> p1 = q1.top();
                q1.pop();
                v--;
                tot += p1.first;
                w.push_back(p1.second+1);
            }
            else {
                pair<int,int> p1 = q1.top();
                q1.pop();
                int k1 = p1.first + q1.top().first;
                int k2 = q2.top().first;
                if (k1>k2) {
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    p1 = q1.top();
                    q1.pop();
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    v -= 2;
                }
                else {
                    q1.push(p1);
                    p1 = q2.top();
                    q2.pop();
                    v -= 2;
                    tot += p1.first;
                    w.push_back(p1.second+1);
                }
            }
        }
        else {
            //cout<<v<<" "<<q1.size()<<" "<<q2.size()<<endl;
            if (q1.size()==1) {
                if (v%2==1) {
                    pair<int,int> p1 = q1.top();
                    q1.pop();
                    v--;
                    tot += p1.first;
                    w.push_back(p1.second+1);
                }
                else {
                    pair<int,int> p1 = q1.top();
                    q1.pop();
                    q2.push(p1);
                }
            }
            else if (q2.size()==0) {
                if (v==0) break;
                if (q1.size()==0) break;
                pair<int,int> p1 = q1.top();
                q1.pop();
                v--;
                tot += p1.first;
                w.push_back(p1.second+1);
            }
            else {
                if (v==1) break;
                if (v==0) break;
                if (q2.size()==0) break;
                pair<int,int> p1 = q2.top();
                q2.pop();
                v-=2;
                tot += p1.first;
                w.push_back(p1.second+1);
            }
        }
    }

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

    return 0;

}


#include <iostream>
#include <sstream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <stdio.h>

using namespace std;

#define fo(i,n) for(int i=0; i<(int)n; i++)
#define rep(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
#define mp(a,b) make_pair(a,b)
#define pb(x) push_back(x)
#define pii pair<int,int>

int n,v;
priority_queue<pair<int,int> > q1,q2;

int main() {

    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n>>v;
    for (int i=0; i<n; i++) {
        int t,p;
        scanf("%d%d",&t,&p);
        if (t==1) {
            q1.push(make_pair(p,i));
        }
        else {
            q2.push(make_pair(p,i));
        }
    }

    int tot = 0;
    vector<int> w;
    while (v>0) {
        if (q1.size()>1 && q2.size()>0 && v>1) {
            if (q1.size()==2 && v%2==1) {
                pair<int,int> p1 = q1.top();
                q1.pop();
                int k1 = p1.first + q1.top().first;
                int k2 = q2.top().first + p1.first;
                if (k1>k2) {
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    p1 = q1.top();
                    q1.pop();
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    v -= 2;
                }
                else {
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    p1 = q2.top();
                    q2.pop();
                    v -= 3;
                    tot += p1.first;
                    w.push_back(p1.second+1);
                }
            }
            else if (q1.size()==3 && v%2==1) {
                pair<int,int> p1 = q1.top();
                q1.pop();
                pair<int,int> p2 = q1.top();
                q1.pop();
                int k1 = p1.first + p2.first + q1.top().first;
                int k2 = q2.top().first + p1.first;
                if (k1>k2) {
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    tot += p2.first;
                    w.push_back(p2.second+1);
                    p1 = q1.top();
                    q1.pop();
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    v -= 2;
                }
                else {
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    p1 = q2.top();
                    q2.pop();
                    v -= 3;
                    tot += p1.first;
                    w.push_back(p1.second+1);
                }
            }
            else {
                pair<int,int> p1 = q1.top();
                q1.pop();
                int k1 = p1.first + q1.top().first;
                int k2 = q2.top().first;
                if (k1>k2) {
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    p1 = q1.top();
                    q1.pop();
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    v -= 2;
                }
                else {
                    q1.push(p1);
                    p1 = q2.top();
                    q2.pop();
                    v -= 2;
                    tot += p1.first;
                    w.push_back(p1.second+1);
                }
            }
        }
        else {
            //cout<<v<<" "<<q1.size()<<" "<<q2.size()<<endl;
            if (q1.size()==1) {
                if (v%2==1) {
                    pair<int,int> p1 = q1.top();
                    q1.pop();
                    v--;
                    tot += p1.first;
                    w.push_back(p1.second+1);
                }
                else {
                    pair<int,int> p1 = q1.top();
                    q1.pop();
                    q2.push(p1);
                }
            }
            else if (q2.size()==0) {
                if (v==0) break;
                if (q1.size()==0) break;
                pair<int,int> p1 = q1.top();
                q1.pop();
                v--;
                tot += p1.first;
                w.push_back(p1.second+1);
            }
            else {
                if (v==1) break;
                if (v==0) break;
                if (q2.size()==0) break;
                pair<int,int> p1 = q2.top();
                q2.pop();
                v-=2;
                tot += p1.first;
                w.push_back(p1.second+1);
            }
        }
    }

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

    return 0;

}


#include <iostream>
#include <sstream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <stdio.h>

using namespace std;

#define fo(i,n) for(int i=0; i<(int)n; i++)
#define rep(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
#define mp(a,b) make_pair(a,b)
#define pb(x) push_back(x)
#define pii pair<int,int>

int n,v;
priority_queue<pair<int,int> > q1,q2;

int main() {

    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n>>v;
    for (int i=0; i<n; i++) {
        int t,p;
        scanf("%d%d",&t,&p);
        if (t==1) {
            q1.push(make_pair(p,i));
        }
        else {
            q2.push(make_pair(p,i));
        }
    }

    int tot = 0;
    vector<int> w;
    while (v>0) {
        if (q1.size()>1 && q2.size()>0) {
            if (q1.size()==2 && v%2==1) {
                pair<int,int> p1 = q1.top();
                q1.pop();
                tot += p1.first;
                w.push_back(p1.second+1);
            }
            else {
                pair<int,int> p1 = q1.top();
                q1.pop();
                int k1 = p1.first + q1.top().first;
                int k2 = q2.top().first;
                if (k1>k2) {
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    p1 = q1.top();
                    q1.pop();
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    v -= 2;
                }
                else {
                    q1.push(p1);
                    p1 = q2.top();
                    q2.pop();
                    v -= 2;
                    tot += p1.first;
                    w.push_back(p1.second+1);
                }
            }
        }
        else {
            cout<<v<<" "<<q1.size()<<" "<<q2.size()<<endl;
            if (q1.size()==1) {
                if (v%2==1) {
                    pair<int,int> p1 = q1.top();
                    q1.pop();
                    v--;
                    tot += p1.first;
                    w.push_back(p1.second+1);
                }
                else {
                    pair<int,int> p1 = q1.top();
                    q1.pop();
                    q2.push(p1);
                }
            }
            else if (q2.size()==0) {
                if (v==0) break;
                if (q1.size()==0) break;
                pair<int,int> p1 = q1.top();
                q1.pop();
                v--;
                tot += p1.first;
                w.push_back(p1.second+1);
            }
            else {
                if (v==1) break;
                if (q2.size()==0) break;
                pair<int,int> p1 = q2.top();
                q2.pop();
                v-=2;
                tot += p1.first;
                w.push_back(p1.second+1);
            }
        }
    }

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

    return 0;

}


#include <iostream>
#include <sstream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <stdio.h>

using namespace std;

#define fo(i,n) for(int i=0; i<(int)n; i++)
#define rep(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
#define mp(a,b) make_pair(a,b)
#define pb(x) push_back(x)
#define pii pair<int,int>

int n,v;
priority_queue<pair<int,int> > q1,q2;

int main() {

    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n>>v;
    for (int i=0; i<n; i++) {
        int t,p;
        scanf("%d%d",&t,&p);
        if (t==1) {
            q1.push(make_pair(p,i));
        }
        else {
            q2.push(make_pair(p,i));
        }
    }

    int tot = 0;
    vector<int> w;
    while (v>0) {
        if (q1.size()>1 && q2.size()>0 && v>1) {
            if (q1.size()==2 && v%2==1) {
                pair<int,int> p1 = q1.top();
                q1.pop();
                int k1 = p1.first + q1.top().first;
                int k2 = q2.top().first + p1.first;
                if (k1>k2) {
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    p1 = q1.top();
                    q1.pop();
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    v -= 2;
                }
                else {
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    p1 = q2.top();
                    q2.pop();
                    v -= 3;
                    tot += p1.first;
                    w.push_back(p1.second+1);
                }
            }
            else {
                pair<int,int> p1 = q1.top();
                q1.pop();
                int k1 = p1.first + q1.top().first;
                int k2 = q2.top().first;
                if (k1>k2) {
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    p1 = q1.top();
                    q1.pop();
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    v -= 2;
                }
                else {
                    q1.push(p1);
                    p1 = q2.top();
                    q2.pop();
                    v -= 2;
                    tot += p1.first;
                    w.push_back(p1.second+1);
                }
            }
        }
        else {
            //cout<<v<<" "<<q1.size()<<" "<<q2.size()<<endl;
            if (q1.size()==1) {
                if (v%2==1) {
                    pair<int,int> p1 = q1.top();
                    q1.pop();
                    v--;
                    tot += p1.first;
                    w.push_back(p1.second+1);
                }
                else {
                    pair<int,int> p1 = q1.top();
                    q1.pop();
                    q2.push(p1);
                }
            }
            else if (q2.size()==0) {
                if (v==0) break;
                if (q1.size()==0) break;
                pair<int,int> p1 = q1.top();
                q1.pop();
                v--;
                tot += p1.first;
                w.push_back(p1.second+1);
            }
            else {
                if (v==1) break;
                if (v==0) break;
                if (q2.size()==0) break;
                pair<int,int> p1 = q2.top();
                q2.pop();
                v-=2;
                tot += p1.first;
                w.push_back(p1.second+1);
            }
        }
    }

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

    return 0;

}


#include <iostream>
#include <sstream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <stdio.h>

using namespace std;

#define fo(i,n) for(int i=0; i<(int)n; i++)
#define rep(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
#define mp(a,b) make_pair(a,b)
#define pb(x) push_back(x)
#define pii pair<int,int>

int n,v;
vector<pair<int,int> > v1,v2;
int sum[100010];

int main() {

    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n>>v;
    for (int i=0; i<n; i++) {
        int t,p;
        scanf("%d%d",&t,&p);
        if (t==1) {
            v1.push_back(make_pair(p,i));
        }
        else {
            v2.push_back(make_pair(p,i));
        }
    }

    sort(v1.begin(),v1.end());
    reverse(v1.begin(),v1.end());
    sort(v2.begin(),v2.end());
    reverse(v2.begin(),v2.end());
    for (int i=0; i<v1.size(); i++) sum[i+1] = sum[i] + v1[i].first;

    int tmp = 0;
    int p = -1;
    int best = 0;
    for (int i=0; i<=v2.size(); i++) {
        int vv = v-2*i;
        if (vv<0) continue;
        vv = min(vv,(int)v1.size());
        int c = tmp + sum[vv];
        if (c>best) {
            best = c;
            p = i;
        }
        if (i<v2.size()) tmp += v2[i].first;
    }
    cout<<best<<endl;
    for (int i=1; i<=p; i++) cout<<v2[i-1].second+1<<" ";
    int vv = v-2*p;
    vv = min(vv,(int)v1.size());
    for (int i=0; i<vv; i++) cout<<v1[i].second+1<<" ";

    return 0;

}


#include <iostream>
#include <sstream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <stdio.h>

using namespace std;

#define fo(i,n) for(int i=0; i<(int)n; i++)
#define rep(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
#define mp(a,b) make_pair(a,b)
#define pb(x) push_back(x)
#define pii pair<int,int>

int n,v;
priority_queue<pair<int,int> > q1,q2;

int main() {

    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n>>v;
    for (int i=0; i<n; i++) {
        int t,p;
        scanf("%d%d",&t,&p);
        if (t==1) {
            q1.push(make_pair(p,i));
        }
        else {
            q2.push(make_pair(p,i));
        }
    }

    int tot = 0;
    vector<int> w;
    while (v>0) {
        if (q1.size()>1 && q2.size()>0 && v>1) {
            if (q1.size()==2 && v%2==1) {
                pair<int,int> p1 = q1.top();
                q1.pop();
                v--;
                tot += p1.first;
                w.push_back(p1.second+1);
            }
            else {
                pair<int,int> p1 = q1.top();
                q1.pop();
                int k1 = p1.first + q1.top().first;
                int k2 = q2.top().first;
                if (v%2==1 && k2>q1.top().first) {
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    p1 = q2.top();
                    q2.pop();
                    v -= 3;
                    tot += p1.first;
                    w.push_back(p1.second+1);
                }
                else if (k1>k2) {
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    p1 = q1.top();
                    q1.pop();
                    tot += p1.first;
                    w.push_back(p1.second+1);
                    v -= 2;
                }
                else {
                    q1.push(p1);
                    p1 = q2.top();
                    q2.pop();
                    v -= 2;
                    tot += p1.first;
                    w.push_back(p1.second+1);
                }
            }
        }
        else {
            //cout<<v<<" "<<q1.size()<<" "<<q2.size()<<endl;
            if (q1.size()==1) {
                if (v%2==1) {
                    pair<int,int> p1 = q1.top();
                    q1.pop();
                    v--;
                    tot += p1.first;
                    w.push_back(p1.second+1);
                }
                else {
                    pair<int,int> p1 = q1.top();
                    q1.pop();
                    q2.push(p1);
                }
            }
            else if (q2.size()==0) {
                if (v==0) break;
                if (q1.size()==0) break;
                pair<int,int> p1 = q1.top();
                q1.pop();
                v--;
                tot += p1.first;
                w.push_back(p1.second+1);
            }
            else {
                if (v==1) break;
                if (v==0) break;
                if (q2.size()==0) break;
                pair<int,int> p1 = q2.top();
                q2.pop();
                v-=2;
                tot += p1.first;
                w.push_back(p1.second+1);
            }
        }
    }

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

    return 0;

}


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

#define N 101000

int n, m;
int first[N], second[N], t[N], p[N], id1[N], id2[N];
int num1[N], num2[N], sum1[N], sum2[N], f[N];

bool cmp1(int i, int j) {
	return first[i] > first[j];
}

bool cmp2(int i, int j) {
	return second[i] > second[j];
}

int main() {
///	freopen("b.in", "r", stdin);
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; i ++) scanf("%d %d", &t[i], &p[i]);
	int gas1 = 0, gas2 = 0;
	for (int i = 1; i <= n; i ++) {
		if (t[i] == 1) first[++gas1] = p[i], num1[gas1] = i;
		else second[++gas2] = p[i], num2[gas2] = i;
	}

	for (int i = 1; i <= gas1; i ++) id1[i] = i;
	sort(id1 + 1, id1 + gas1 + 1, cmp1);
	for (int i = 1; i <= gas2; i ++) id2[i] = i;
	sort(id2 + 1, id2 + gas2 + 1, cmp2);

	sum1[0] = sum2[0] = 0;
	for (int i = 1; i <= gas1; i ++) sum1[i] = sum1[i-1] + first[id1[i]];
	for (int i = 1; i <= gas2; i ++) sum2[i] = sum2[i-1] + second[id2[i]];

	int en = min(m / 2, gas2);
	for (int i = 0; i <= en; i ++) f[i] = sum2[i] + sum1[min(m-2*i,gas1)];

	int mx = 0, ans = 0;
	for (int i = 0; i <= en; i ++) {
		if (mx < f[i]) mx = f[i], ans = i;
	}
	printf("%d\n", mx);
	for (int i = 1; i <= ans; i ++) printf("%d ", num2[id2[i]]);
	for (int i = 1; i <= min(gas1, m - ans * 2); i ++) printf("%d ", num1[id1[i]]);
	puts("");
}


#include <stdio.h>
#include <memory.h>
#include <algorithm>
using namespace std;
#define N 100011
#define min(a, b) ((a) < (b) ? (a) : (b))

struct dat{
    int pos, c;
    dat(int pos = 0, int c = 0): pos(pos), c(c){}
}p1[N], p2[N];

bool cmp(dat a, dat b) {
    return a.c > b.c;
}

int n, n1, n2, v;
int tot1[N], tot2[N];

int main(){
    //freopen("b.in", "r", stdin);
    scanf("%d %d", &n, &v);
    n1 = 0, n2 = 0;
    for(int i=0; i<n; i++){
        int x, y;
        scanf("%d %d", &x, &y);
        if(x == 1) p1[++n1].c = y, p1[n1].pos = i+1;
        else p2[++n2].c = y, p2[n2].pos = i+1;
    }

    sort(p1+1, p1+n1+1, cmp);
    sort(p2+1, p2+n2+1, cmp);

    tot2[0] = 0;
    for(int i=1; i<=n2; i++) tot2[i] = tot2[i-1] + p2[i].c;
    tot1[0] = 0;
    for(int i=1; i<=n1; i++) tot1[i] = tot1[i-1] + p1[i].c;

    int mx = 0, k2 = -1, k1 = -1;
    for(int i=0; i<=n2; i++) {
        if(i*2 > v) break;
        int j = min(v-2*i, n1);
        if(mx < tot2[i] + tot1[j]) mx = tot2[i] + tot1[j], k2 = i, k1 = j;
    }

    printf("%d\n", mx);
    for(int i=1; i<=k1; i++) printf("%d ", p1[i].pos);
    for(int i=1; i<=k2; i++) printf("%d ", p2[i].pos);

    return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;
#define N 100050
int n, v, a[N], b[N], g1, g2, p, x, c[N];

bool cmp(int x, int y){
    return x > y;
}

int main(){
    scanf("%d %d", &n, &v);
    g1 = g2 = 0;
    for(int i = 0; i < n; i ++){
        scanf("%d %d", &p, &x);
        if(p == 1) a[g1 ++] = x;
        else b[g2 ++] = x;
    }
    sort(a, a + g1, cmp);
    long long ans = 0;
    if( v >= (g1 + 2 * g2) ) {
        for(int i = 0; i < g1; i ++) ans += a[i];
        for(int i = 0; i < g2; i ++) ans += b[i];
        printf("%d\n", ans);
        printf("%d\n", g1 + 2 * g2);
    }
    else {
        if(v % 2) {
            ans += a[0];
            int  G1 = (g1 - 1) / 2;
            for(int i = 0; i < G1; i ++) b[i + g2] = a[2 * i + 1] + a[2 * i + 2];
            g2 += G1;
        }
        else{
            int G1 = g1 / 2;
            for(int i = 0; i < G1; i ++) b[i + g2] = a[2 * i] + a[2 * i + 1];
            g2 += G1;
        }
        sort(b, b + g2, cmp);
        for(int i = 0; i < (v / 2); i ++ ){
            ans += b[i];

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


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

#define  N 100100
int t[N],p[N],id[N],res;
bool cmp(int i,int j){
    return p[i]*2/t[i]>p[j]*2/t[j];
}
int n,v;
void input()
{
    scanf("%d%d",&n,&v);
    for(int i=0;i<n;i++){
        scanf("%d%d",&t[i],&p[i]);
    }
}
void solve()
{
    for(int i=0;i<n;i++)id[i]=i;
    sort(id,id+n,cmp);
    int i,ii=-1,sumv=0;
    res=0;
    for(i=0;i<n;i++){
        int j=id[i];
        sumv+=t[j];
        if(sumv>v){sumv-=t[j];break;}
        res+=p[j];
    }
    if(sumv<v){
        for(ii=i;ii<n;ii++){
            int j=id[ii];
            if(t[j]==1){res+=p[j];break;}
        }
        if(ii<n)id[i++]=id[ii];
    }
    printf("%d\n",res);
    for(int j=0;j<i;j++){
        printf("%d ",id[j]+1);
    }
    puts("");
}
int main()
{
 ///   freopen("in.txt","r",stdin);
    input();
    solve();
    return 0;
}


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

#define N 101000

int n, m;
int first[N], second[N], t[N], p[N], id1[N], id2[N];
int num1[N], num2[N], sum1[N], sum2[N], f[N];

bool cmp1(int i, int j) {
	return first[i] > first[j];
}

bool cmp2(int i, int j) {
	return second[i] > second[j];
}

int main() {
///	freopen("b.in", "r", stdin);
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; i ++) scanf("%d %d", &t[i], &p[i]);
	int gas1 = 0, gas2 = 0;
	for (int i = 1; i <= n; i ++) {
		if (t[i] == 1) first[++gas1] = p[i], num1[gas1] = i;
		else second[++gas2] = p[i], num2[gas2] = i;
	}

	for (int i = 1; i <= gas1; i ++) id1[i] = i;
	sort(id1 + 1, id1 + gas1 + 1, cmp1);
	for (int i = 1; i <= gas2; i ++) id2[i] = i;
	sort(id2 + 1, id2 + gas2 + 1, cmp2);

	sum1[0] = sum2[0] = 0;
	for (int i = 1; i <= gas1; i ++) sum1[i] = sum1[i-1] + first[id1[i]];
	for (int i = 1; i <= gas2; i ++) sum2[i] = sum2[i-1] + second[id2[i]];
	for (int i = 0; i <= m / 2; i ++) f[i] = sum2[i] + sum1[m-2*i];

	int mx = 0, ans = 0;
	for (int i = 0; i <= m / 2; i ++) {
		if (mx < f[i]) mx = f[i], ans = i;
	}
	printf("%d\n", mx);
	for (int i = 1; i <= ans; i ++) printf("%d ", num2[id2[i]]);
	for (int i = 1; i <= m - ans * 2; i ++) printf("%d ", num1[id1[i]]);
	puts("");
}


#include<stdio.h>
#include<algorithm>
using namespace std;
#define N 110000
int a[N], b[N];
int p[N];
int id[N];
bool cmp(int i, int j){
    return b[i] * a[j] > b[j] * a[i];
}
int main(){
    int n, v;
    //freopen("B.in","r",stdin);
    while(scanf("%d%d", &n, &v) == 2){
        for(int i = 0; i < n; i ++) {
            scanf("%d%d", &a[i], &b[i]);
            id[i] = i;
        }
        sort(id, id + n, cmp);
        int cnt = 0, sum = 0, sum0 = 0;
        for(int i = 0; i < n; i ++){
            sum0 += a[id[i]];
            if(sum0 > v)break;
            p[cnt ++] = id[i] + 1;
            sum += b[id[i]];
        }
        printf("%d\n", sum);
        for(int i = 0; i < cnt; i ++)printf("%d ", p[i]);
        puts("");
    }
}


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

#define  N 100100
int t[N],p[N],id[N],res;
bool cmp(int i,int j){
    if(p[i]*2/t[i]>p[j]*2/t[j])return true;
    if(p[i]*2/t[i]<p[j]*2/t[j])return false;
    return t[i]>t[j];
}
int n,v;
void input()
{
    scanf("%d%d",&n,&v);
    for(int i=0;i<n;i++){
        scanf("%d%d",&t[i],&p[i]);
    }
}
void solve()
{
    for(int i=0;i<n;i++)id[i]=i;
    sort(id,id+n,cmp);
    int i,ii=-1,sumv=0;
    res=0;
    for(i=0;i<n;i++){
        int j=id[i];
        sumv+=t[j];
        if(sumv>v){sumv-=t[j];break;}
        res+=p[j];
    }
    if(sumv<v){
        for(ii=i;ii<n;ii++){
            int j=id[ii];
            if(t[j]==1){res+=p[j];break;}
        }
        if(ii<n)id[i++]=id[ii];
    }
    printf("%d\n",res);
    for(int j=0;j<i;j++){
        printf("%d ",id[j]+1);
    }
    puts("");
}
int main()
{
 ///   freopen("in.txt","r",stdin);
    input();
    solve();
    return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;
#define N 100050
int n, v, a[N], b[N], g1, g2, p, x, c[N];

bool cmp(int x, int y){
    return x > y;
}

int main(){
    scanf("%d %d", &n, &v);
    g1 = g2 = 0;
    for(int i = 0; i < n; i ++){
        scanf("%d %d", &p, &x);
        if(p == 1) a[g1 ++] = x;
        else b[g2 ++] = x;
    }
    sort(a, a + g1, cmp);
    long long ans = 0;
    if( v >= (g1 + 2 * g2) ) {
        for(int i = 0; i < g1; i ++) ans += a[i];
        for(int i = 0; i < g2; i ++) ans += b[i];
        printf("%d\n", ans);
        printf("%d\n", g1 + 2 * g2);
    }
    else {
        if(v % 2) {
            ans += a[0];
            int  G1 = (g1 - 1) / 2;
            for(int i = 0; i < G1; i ++) b[i + g2] = a[2 * i + 1] + a[2 * i + 2];
            g2 += G1;
        }
        else{
            int G1 = g1 / 2;
            for(int i = 0; i < G1; i ++) b[i + g2] = a[2 * i] + a[2 * i + 1];
            g2 += G1;
        }
        sort(b, b + g2, cmp);
        for(int i = 0; i < (v / 2); i ++ ){
            ans += b[i];
            printf("%d ", b[i]);
        }
        printf("%d\n", ans);
        printf("%d\n", v);
    }
}


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

#define N 101000

int n, m;
int first[N], second[N], t[N], p[N], id1[N], id2[N];
int num1[N], num2[N], sum1[N], sum2[N], f[N];

bool cmp1(int i, int j) {
	return first[i] > first[j];
}

bool cmp2(int i, int j) {
	return second[i] > second[j];
}

int main() {
///	freopen("b.in", "r", stdin);
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; i ++) scanf("%d %d", &t[i], &p[i]);
	int gas1 = 0, gas2 = 0;
	for (int i = 1; i <= n; i ++) {
		if (t[i] == 1) first[++gas1] = p[i], num1[gas1] = i;
		else second[++gas2] = p[i], num2[gas2] = i;
	}

	for (int i = 1; i <= gas1; i ++) id1[i] = i;
	sort(id1 + 1, id1 + gas1 + 1, cmp1);
	for (int i = 1; i <= gas2; i ++) id2[i] = i;
	sort(id2 + 1, id2 + gas2 + 1, cmp2);

	sum1[0] = sum2[0] = 0;
	for (int i = 1; i <= gas1; i ++) sum1[i] = sum1[i-1] + first[id1[i]];
	for (int i = 1; i <= gas2; i ++) sum2[i] = sum2[i-1] + second[id2[i]];

	int en = min(m / 2, gas2);
	for (int i = 0; i <= m / 2; i ++) f[i] = sum2[i] + sum1[min(m-2*i,gas1)];

	int mx = 0, ans = 0;
	for (int i = 0; i <= en; i ++) {
		if (mx < f[i]) mx = f[i], ans = i;
	}
	printf("%d\n", mx);
	for (int i = 1; i <= ans; i ++) printf("%d ", num2[id2[i]]);
	for (int i = 1; i <= m - ans * 2; i ++) printf("%d ", num1[id1[i]]);
	puts("");
}


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

#define N 101000

int n, m;
int first[N], second[N], t[N], p[N], id1[N], id2[N];
int num1[N], num2[N], sum1[N], sum2[N], f[N];

bool cmp1(int i, int j) {
	return first[i] > first[j];
}

bool cmp2(int i, int j) {
	return second[i] > second[j];
}

int main() {
///	freopen("b.in", "r", stdin);
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; i ++) scanf("%d %d", &t[i], &p[i]);
	int gas1 = 0, gas2 = 0;
	for (int i = 1; i <= n; i ++) {
		if (t[i] == 1) first[++gas1] = p[i], num1[gas1] = i;
		else second[++gas2] = p[i], num2[gas2] = i;
	}

	for (int i = 1; i <= gas1; i ++) id1[i] = i;
	sort(id1 + 1, id1 + gas1 + 1, cmp1);
	for (int i = 1; i <= gas2; i ++) id2[i] = i;
	sort(id2 + 1, id2 + gas2 + 1, cmp2);

	sum1[0] = sum2[0] = 0;
	for (int i = 1; i <= gas1; i ++) sum1[i] = sum1[i-1] + first[id1[i]];
	for (int i = 1; i <= gas2; i ++) sum2[i] = sum2[i-1] + second[id2[i]];

	int en = min(m / 2, gas2);
	for (int i = 0; i <= m / 2; i ++) f[i] = sum2[i] + sum1[min(m-2*i,gas1)];

	int mx = 0, ans = 0;
	for (int i = 0; i <= en; i ++) {
		if (mx < f[i]) mx = f[i], ans = i;
	}
	printf("%d\n", mx);
	for (int i = 1; i <= ans; i ++) printf("%d ", num2[id2[i]]);
	for (int i = 1; i <= min(gas1, m - ans * 2); i ++) printf("%d ", num1[id1[i]]);
	puts("");
}


#include <stdio.h>
#include <memory.h>
#include <algorithm>
using namespace std;
#define N 100011

int n, len;
int le[N], c[N], id[N];
bool vis[N];

bool cmp(int i, int j){
    if(c[i] * le[j] != c[j] * le[i]) return c[i] * le[j] < c[j] * le[i];
    return le[i] > le[j];
}

int main(){
    //freopen("b.in", "r", stdin);
    scanf("%d %d", &n, &len);
    int cnt1 = 0, cnt2 = 0;
    for(int i=1; i<=n; i++){
        scanf("%d %d", &le[i], &c[i]);
        id[i] = i;
    }
    sort(id+1, id+n+1, cmp);
    memset(vis, 0, sizeof(vis));

    int tot = 0, num = n + 1, sum = 0;
    while(1){
        if(tot >= len) break;
        num --;
        if(!num) break;
        tot += le[id[num]];
        if(tot > len) tot -= le[id[num]];
        else {
            vis[num] = 1;
            sum += c[id[num]];
        }
    }

    printf("%d\n", sum);
    for(int i=num; i<=n; i++) if(vis[i]) printf("%d ", id[i]);

    return 0;
}