#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <sstream>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <functional>
#include <utility>
#include <stdio.h>
#define ll long long
#define MP make_pair
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define PLI pair<ll, int>
#define PIL pair<int, ll>
#define F first
#define S second
#define SHN 1e9
#define LSHN 1e18
using namespace std;
ll n,m,k;
ll a[100010],b[100010];
ll l,r,mid;
ll res;
int main()
{
	cin >>n>>m>>k;
	for (int i=0;i<n;i++) cin >>a[i];
	for (int i=0;i<m;i++) cin >>b[i];
	sort(a,a+n);
	sort(b,b+m);
	l=0,r=min(n,m)+1;
	while (l+1<r)
	{
		mid=(l+r)/2;
		ll t=0;
		for (int i=0;i<mid;i++) t+=max((ll)0,b[i]-a[n-mid+i]);
		if (t<=k) l=mid;
		else r=mid;
	}
	cout <<l<<" ";
	for (int i=0;i<l;i++)
	{
		ll t=max((ll)0,b[i]-a[n-l+i]);
		k-=t;
		b[i]-=t;
	}
	for (int i=0;i<l;i++)
	{
		ll t=min(k,b[i]);
		k-=t;
		b[i]-=t;
		res+=b[i];
		//cout <<b[i]<<" "<<k<<" "<<res<<" ##\n";
	}
	cout <<res;
	return 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <sstream>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <functional>
#include <utility>
#include <stdio.h>
#define ll long long
#define MP make_pair
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define PLI pair<ll, int>
#define PIL pair<int, ll>
#define F first
#define S second
#define SHN 1e9
#define LSHN 1e18
using namespace std;
ll n,m,k;
ll a[100010],b[100010];
ll l,r,mid;
int main()
{
	cin >>n>>m>>k;
	for (int i=0;i<n;i++) cin >>a[i];
	for (int i=0;i<m;i++) cin >>b[i];
	sort(a,a+n);
	reverse(a,a+n);
	sort(b,b+m);
	l=-1,r=min(n,m);
	while (l<r-1)
	{
		mid=(l+r)>>1;
		ll sum=0;
		for (int i=0;i<=mid;i++) sum+=max((ll)0,b[mid-i]-a[i]);
		//cout <<l<<" "<<r<<" "<<mid<<" "<<sum<<" ##\n";
		if (sum<=k) l=mid;
		else r=mid;
	}
	cout <<l+1<<" ";
	ll sum=0;
	for (int i=0;i<=l;i++) sum+=b[i];
	cout <<sum-k;
	return 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <sstream>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <functional>
#include <utility>
#include <stdio.h>
#define ll long long
#define MP make_pair
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define PLI pair<ll, int>
#define PIL pair<int, ll>
#define F first
#define S second
#define SHN 1e9
#define LSHN 1e18
using namespace std;
ll l,r,mid;
ll n,m,k;
ll r1,r2;
ll a[100010],b[100010];
bool check()
{
	ll t=0;
	for (int i=n-1,j=mid-1;i>=0,j>=0;i--,j--)
	{
		if (a[i]<b[j]) t+=b[j]-a[i];
		//cout <<t<<" "<<a[i]<<" "<<b[j]<<" #\n";
		if (t>k) return 0;
	}
	return 1;
}
int main()
{
	cin >>n>>m>>k;
	for (int i=0;i<n;i++) cin >>a[i];
	for (int i=0;i<m;i++) cin >>b[i];
	sort(a,a+n);
	sort(b,b+m);
	l=0,r=min(n,m);
	while (l<=r)
	{
		mid=(l+r)/2;
		//cout <<l<<" "<<r<<" "<<mid<<" ##\n";
		if (check()) l=mid+1,r1=mid;
		else r=mid-1;
	}
	for (int i=n-1,j=r1-1;i>=0,j>=0;i--,j--)
	{
		if (a[i]<b[j])
		{
			k-=b[j]-a[i];
			r2+=a[i];
		}
		else r2+=b[j];
	}
	cout <<r1<<" "<<max((ll)0,r2-k);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> bike;
vector<ll> boy;
ll n,m,a,hehe;
bool can(int mid){
	ll need=0;
	//cout<<mid<<endl;
	for(int i=0;i<mid;i++){
		//cout<<bike[i]<<" "<<boy[n-mid+i]<<endl;
		if(bike[i]>boy[n-mid+i]){
			need+=(bike[i]-boy[n-mid+i]);
		}
	}
	//cout<<need<<endl;
	return need<=a;
}
int main(){
	bike.clear();boy.clear();
	cin>>n>>m>>a;
	ll data;
	for(int i=0;i<n;i++){
		cin>>data;
		boy.push_back(data);
	}
	for(int i=0;i<m;i++){
		cin>>data;
		bike.push_back(data);
	}
	sort(boy.begin(),boy.end());
	sort(bike.begin(),bike.end());
	ll l,r,mid;
	l=0,r=n+1;
	ll ans=0;
	while(l<=r){
		mid=(l+r)>>1;
		if(mid>n){
			r--;
			continue;
		}
		if(can(mid)){
			ans=max(mid,ans);
			l=mid+1;
		}else{
			r=mid-1;
		}
	}
	cout<<ans<<" ";
	if(ans==0){
		cout<<0<<endl;
		return 0;
	}
	hehe=0;
	ll haha=0;
	for(int i=0;i<ans;i++){
		if(bike[i]>boy[n-ans+i]){
			hehe+=bike[i]-boy[n-ans+i];
			haha+=boy[n-ans+i];
		}else{
			hehe+=bike[i];
		}
	}
	hehe-=a;
	if((hehe+haha)<0)
		cout<<0<<endl;
	else
		cout<<hehe+haha<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> bike;
vector<ll> boy;
ll n,m,a,hehe;
bool can(int mid){
	ll need=0;
	//cout<<mid<<endl;
	for(int i=0;i<mid;i++){
		//cout<<bike[i]<<" "<<boy[n-mid+i]<<endl;
		if(bike[i]>boy[n-mid+i]){
			need+=(bike[i]-boy[n-mid+i]);
		}
	}
	//cout<<need<<endl;
	return need<=a;
}
int main(){
	bike.clear();boy.clear();
	cin>>n>>m>>a;
	ll data;
	for(int i=0;i<n;i++){
		cin>>data;
		boy.push_back(data);
	}
	for(int i=0;i<m;i++){
		cin>>data;
		bike.push_back(data);
	}
	sort(boy.begin(),boy.end());
	sort(bike.begin(),bike.end());
	ll l,r,mid;
	l=0,r=n+1;
	ll ans=0;
	for(int i=0;i<n;i++){
		mid=i;
		if(mid>n){
			r--;
			continue;
		}
		if(can(mid)){
			ans=max(mid,ans);
			l=mid+1;
		}else{
			r=mid-1;
		}
	}
	cout<<ans<<" ";
	if(ans==0){
		cout<<0<<endl;
		return 0;
	}
	hehe=0;
	ll haha=0;
	for(int i=0;i<ans;i++){
		if(bike[i]>boy[n-ans+i]){
			hehe+=boy[n-ans+i];
			haha+=bike[i]-boy[n-ans+i];
		}else{
			hehe+=bike[i];
		}
	}
	haha-=a;
	if((hehe+haha)<0)
		cout<<0<<endl;
	else
		cout<<hehe+haha<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> bike;
vector<ll> boy;
ll n,m,a,hehe;
bool can(int mid){
	ll need=0;
	//cout<<mid<<endl;
	for(int i=0;i<mid;i++){
		//cout<<bike[i]<<" "<<boy[n-mid+i]<<endl;
		if(bike[i]>boy[n-mid+i]){
			need+=(bike[i]-boy[n-mid+i]);
		}
	}
	//cout<<need<<endl;
	return need<=a;
}
int main(){
	bike.clear();boy.clear();
	cin>>n>>m>>a;
	ll data;
	for(int i=0;i<n;i++){
		cin>>data;
		boy.push_back(data);
	}
	for(int i=0;i<m;i++){
		cin>>data;
		bike.push_back(data);
	}
	sort(boy.begin(),boy.end());
	sort(bike.begin(),bike.end());
	ll l,r,mid;
	l=0,r=n+1;
	ll ans=0;
	while(l<=r){
		mid=(l+r)>>1;
		if(mid>n){
			r--;
			continue;
		}
		if(can(mid)){
			ans=max(mid,ans);
			l=mid+1;
		}else{
			r=mid-1;
		}
	}
	cout<<ans<<" ";
	if(ans==0){
		cout<<0<<endl;
		return 0;
	}
	hehe=0;
	ll haha=0;
	for(int i=0;i<ans;i++){
		if(bike[i]>boy[n-ans+i]){
			hehe+=boy[n-ans+i];
			haha+=bike[i]-boy[n-ans+i];
		}else{
			hehe+=bike[i];
		}
	}
	haha-=a;
	if((hehe+haha)<0)
		cout<<0<<endl;
	else
		cout<<hehe+haha<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> bike;
vector<ll> boy;
ll n,m,a,hehe;
bool can(int mid){
	ll need=0;
	//cout<<mid<<endl;
	for(int i=0;i<mid;i++){
		//cout<<bike[i]<<" "<<boy[n-mid+i]<<endl;
		if(bike[i]>boy[n-mid+i]){
			need+=(bike[i]-boy[n-mid+i]);
		}
	}
	//cout<<need<<endl;
	return need<=a;
}
int main(){
	bike.clear();boy.clear();
	cin>>n>>m>>a;
	ll data;
	for(int i=0;i<n;i++){
		cin>>data;
		boy.push_back(data);
	}
	for(int i=0;i<m;i++){
		cin>>data;
		bike.push_back(data);
	}
	sort(boy.begin(),boy.end());
	sort(bike.begin(),bike.end());
	ll l,r,mid;
	l=0,r=n+1;
	ll ans=0;
	while(l<=r){
		mid=(l+r)>>1;
		if(mid>=n){
			r--;
			continue;
		}
		if(can(mid)){
			ans=max(mid,ans);
			l=mid+1;
		}else{
			r=mid-1;
		}
	}
	cout<<ans<<" ";
	if(ans==0){
		cout<<0<<endl;
		return 0;
	}
	hehe=0;
	ll haha=0;
	for(int i=0;i<ans;i++){
		if(bike[i]>boy[n-mid+i]){
			hehe+=bike[i]-boy[n-mid+i];
			haha+=boy[n-mid+i];
		}else{
			hehe+=bike[i];
		}
	}
	hehe-=a;
	if((hehe+haha)<0)
		cout<<0<<endl;
	else
		cout<<hehe+haha<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> bike;
vector<ll> boy;
ll n,m,a,hehe;
bool can(ll mid){
	ll need=0;
	//cout<<mid<<endl;
	for(int i=0;i<mid;i++){
		//cout<<bike[i]<<" "<<boy[n-mid+i]<<endl;
		if(bike[i]>boy[n-mid+i]){
			need+=(bike[i]-boy[n-mid+i]);
		}
	}
	//cout<<need<<endl;
	return need<=a;
}
int main(){
	bike.clear();boy.clear();
	cin>>n>>m>>a;
	ll data;
	for(int i=0;i<n;i++){
		cin>>data;
		boy.push_back(data);
	}
	for(int i=0;i<m;i++){
		cin>>data;
		bike.push_back(data);
	}
	sort(boy.begin(),boy.end());
	sort(bike.begin(),bike.end());
	ll l,r,mid;
	l=0,r=min(n,m);
	ll ans=0;
	while(l<=r){
		mid=(l+r)>>1;
		if(can(mid)){
			ans=max(mid,ans);
			l=mid+1;
		}else{
			r=mid-1;
		}
	}
	cout<<ans<<" ";
	if(ans==0){
		cout<<0<<endl;
		return 0;
	}
	hehe=0;
	ll haha=0;
	for(int i=0;i<ans;i++){
		if(bike[i]>boy[n-ans+i]){
			hehe+=boy[n-ans+i];
			haha+=bike[i]-boy[n-ans+i];
		}else{
			hehe+=bike[i];
		}
	}
	haha-=a;
	if((hehe+haha)<0)
		cout<<0<<endl;
	else
		cout<<hehe+haha<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> bike;
vector<ll> boy;
ll n,m,a,hehe;
bool can(int mid){
	ll need=0;
	//cout<<mid<<endl;
	for(int i=0;i<mid;i++){
		//cout<<bike[i]<<" "<<boy[n-mid+i]<<endl;
		if(bike[i]>boy[n-mid+i]){
			need+=(bike[i]-boy[n-mid+i]);
		}
	}
	//cout<<need<<endl;
	return need<=a;
}
int main(){
	bike.clear();boy.clear();
	cin>>n>>m>>a;
	ll data;
	for(int i=0;i<n;i++){
		cin>>data;
		boy.push_back(data);
	}
	for(int i=0;i<m;i++){
		cin>>data;
		bike.push_back(data);
	}
	sort(boy.begin(),boy.end());
	sort(bike.begin(),bike.end());
	ll l,r,mid;
	l=0,r=n+1;
	ll ans=0;
	while(l<=r){
		mid=(l+r)>>1;
		if(mid>n){
			r--;
			continue;
		}
		if(can(mid)){
			ans=max(mid,ans);
			l=mid+1;
		}else{
			r=mid-1;
		}
	}
	cout<<ans<<" ";
	if(ans==0){
		cout<<0<<endl;
		return 0;
	}
	hehe=0;
	ll haha=0;
	for(int i=0;i<ans;i++){
		if(bike[i]>boy[n-mid+i]){
			hehe+=bike[i]-boy[n-mid+i];
			haha+=boy[n-mid+i];
		}else{
			hehe+=bike[i];
		}
	}
	hehe-=a;
	if((hehe+haha)<0)
		cout<<0<<endl;
	else
		cout<<hehe+haha<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> bike;
vector<ll> boy;
ll n,m,a,hehe;
bool can(int mid){
	ll need=0;
	//cout<<mid<<endl;
	for(int i=0;i<mid;i++){
		//cout<<bike[i]<<" "<<boy[n-mid+i]<<endl;
		if(bike[i]>boy[n-mid+i]){
			need+=(bike[i]-boy[n-mid+i]);
		}
	}
	//cout<<need<<endl;
	return need<=a;
}
int main(){
	bike.clear();boy.clear();
	cin>>n>>m>>a;
	ll data;
	for(int i=0;i<n;i++){
		cin>>data;
		boy.push_back(data);
	}
	for(int i=0;i<m;i++){
		cin>>data;
		bike.push_back(data);
	}
	sort(boy.begin(),boy.end());
	sort(bike.begin(),bike.end());
	ll l,r,mid;
	l=0,r=n+1;
	ll ans=0;
	for(int i=0;i<=n;i++){
		mid=i;
		if(can(mid)){
			ans=max(mid,ans);
			l=mid+1;
		}else{
			r=mid-1;
		}
	}
	cout<<ans<<" ";
	if(ans==0){
		cout<<0<<endl;
		return 0;
	}
	hehe=0;
	ll haha=0;
	for(int i=0;i<ans;i++){
		if(bike[i]>boy[n-ans+i]){
			hehe+=boy[n-ans+i];
			haha+=bike[i]-boy[n-ans+i];
		}else{
			hehe+=bike[i];
		}
	}
	haha-=a;
	if((hehe+haha)<0)
		cout<<0<<endl;
	else
		cout<<hehe+haha<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> bike;
vector<ll> boy;
ll n,m,a,hehe;
bool can(int mid){
	ll need=0;
	//cout<<mid<<endl;
	for(int i=0;i<mid;i++){
		//cout<<bike[i]<<" "<<boy[n-mid+i]<<endl;
		if(bike[i]>boy[n-mid+i]){
			need+=(bike[i]-boy[n-mid+i]);
		}
	}
	//cout<<need<<endl;
	return need<=a;
}
int main(){
	bike.clear();boy.clear();
	cin>>n>>m>>a;
	ll data;
	for(int i=0;i<n;i++){
		cin>>data;
		boy.push_back(data);
	}
	for(int i=0;i<m;i++){
		cin>>data;
		bike.push_back(data);
	}
	sort(boy.begin(),boy.end());
	sort(bike.begin(),bike.end());
	ll l,r,mid;
	l=0,r=n;
	ll ans=0;
	while(l<=r){
		mid=(l+r)>>1;
		if(can(mid)){
			ans=max(mid,ans);
			l=mid+1;
		}else{
			r=mid-1;
		}
	}
	cout<<ans<<" ";
	if(ans==0){
		cout<<0<<endl;
		return 0;
	}
	hehe=0;
	ll haha=0;
	for(int i=0;i<ans;i++){
		if(bike[i]>boy[n-mid+i]){
			hehe+=bike[i]-boy[n-mid+i];
			haha+=boy[n-mid+i];
		}else{
			hehe+=bike[i];
		}
	}
	hehe-=a;
	if((hehe+haha)<0)
		cout<<0<<endl;
	else
		cout<<hehe+haha<<endl;
}


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

const int N = (int)1e5+5;
int n, m, a;
int b[N], p[N];

bool cek(int x) {
	if (x > m) return 0;
	int tmpa = a;
	for (int i = 0; i < x; i++) {
//		printf(" > %d %d\n", b[n-x+i], p[i]);
		if (b[n-x+i] < p[i]) tmpa -= (p[i]-b[n-x+i]);
	}
//	printf(">> cek %d %d\n", x, tmpa);
	if (tmpa >= 0) return 1;
	else return 0;
}

int main() {
	scanf("%d %d %d", &n, &m, &a);
	for (int i = 0; i < n; i++) scanf("%d", &b[i]);
	for (int i = 0; i < m; i++) scanf("%d", &p[i]);
	sort(b, b+n);
	sort(p, p+m);

	int l = 0, r = min(n, m), mid, maks = 0;

	for (int i = 0; i < 30; i++) {
		mid = (l+r)/2;
		if (cek(mid)) {
			maks = max(maks, mid);
			l = mid+1;
		}
		else {
			r = mid-1;
		}
	}
//	printf(">> maks = %d\n", maks);
	if (maks == 0) {
		printf("0 0\n");
		return 0;
	}

	int personal = 0, total = 0;
	for (int i = 0; i < maks; i++) {
		total += p[i];
	}
	personal = total-a;
	if (personal < 0) personal = 0;
	printf("%d %d\n", maks, personal);

	return 0;
}

/*
3 3 3
1 1 2
3 5 6
1 0
*/


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

const int N = (int)1e5+5;
int n, m, a;
int b[N], p[N];

bool cek(int x) {
	if (x > m) return 0;
	int tmpa = a;
	for (int i = 0; i < x-1; i++) {
		if (b[i] < p[i]) tmpa -= p[i]-b[n-x+i];
	}

	if (tmpa >= 0) return 1;
	else return 0;
}

int main() {
	scanf("%d %d %d", &n, &m, &a);
	for (int i = 0; i < n; i++) scanf("%d", &b[i]);
	for (int i = 0; i < m; i++) scanf("%d", &p[i]);
	sort(b, b+n);
	sort(p, p+m);

	int l = 0, r = n, mid, maks = 0;

	for (int i = 0; i < 30; i++) {
		mid = (l+r)/2;
		if (cek(mid)) {
			maks = max(maks, mid);
			l = mid+1;
		}
		else {
			r = mid-1;
		}
	}

	int personal = 0, total = 0;
	for (int i = 0; i < maks; i++) {
		total += p[i];
	}
	personal = total-a;
	if (personal < 0) personal = 0;
	printf("%d %d\n", maks, personal);

	return 0;
}


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

typedef long long ll;

const int N = (int)1e5+5;
int n, m, a;
int b[N], p[N];

bool cek(int x) {
	if (x > m) return 0;
	ll tmpa = (ll)a;
	for (int i = 0; i < x; i++) {
//		printf(" > %d %d\n", b[n-x+i], p[i]);
		if (b[n-x+i] < p[i]) tmpa -= (ll)(p[i]-b[n-x+i]);
	}
//	printf(">> cek %d %d\n", x, tmpa);
	if (tmpa >= 0) return 1;
	else return 0;
}

int main() {
	scanf("%d %d %d", &n, &m, &a);
	for (int i = 0; i < n; i++) scanf("%d", &b[i]);
	for (int i = 0; i < m; i++) scanf("%d", &p[i]);
	sort(b, b+n);
	sort(p, p+m);

	int l = 0, r = min(n, m), mid, maks = 0;

	for (int i = 0; i < 30; i++) {
		mid = (l+r)/2;
		if (cek(mid)) {
			maks = max(maks, mid);
			l = mid+1;
		}
		else {
			r = mid-1;
		}
	}
//	printf(">> maks = %d\n", maks);
	if (maks == 0) {
		printf("0 0\n");
		return 0;
	}

	int personal = 0, total = 0;
	for (int i = 0; i < maks; i++) {
		total += p[i];
	}
	personal = total-a;
	if (personal < 0) personal = 0;
	printf("%d %d\n", maks, personal);

	return 0;
}

/*
3 3 3
1 1 2
3 5 6
1 0
*/


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

const int N = (int)1e5+5;
int n, m, a;
int b[N], p[N];

bool cek(int x) {
	if (x > m) return 0;
	int tmpa = a;
	for (int i = 0; i < x-1; i++) {
		if (b[i] < p[i]) tmpa -= p[i]-b[n-x+i];
	}

	if (tmpa >= 0) return 1;
	else return 0;
}

int main() {
	scanf("%d %d %d", &n, &m, &a);
	for (int i = 0; i < n; i++) scanf("%d", &b[i]);
	for (int i = 0; i < m; i++) scanf("%d", &p[i]);
	sort(b, b+n);
	sort(p, p+m);

	int l = 0, r = n, mid, maks = 0;

	for (int i = 0; i < 30; i++) {
		mid = (l+r)/2;
		if (cek(mid)) {
			maks = max(maks, mid);
			l = mid+1;
		}
		else {
			r = mid-1;
		}
	}

	if (maks == 0) {
		printf("0 0\n");
		return 0;
	}

	int personal = 0, total = 0;
	for (int i = 0; i < maks; i++) {
		total += p[i];
	}
	personal = total-a;
	if (personal < 0) personal = 0;
	printf("%d %d\n", maks, personal);

	return 0;
}


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

const int N = (int)1e5+5;
int n, m, a;
int b[N], p[N];

bool cek(int x) {
	if (x > m) return 0;
	int tmpa = a;
	for (int i = 0; i < x; i++) {
//		printf(" > %d %d\n", b[n-x+i], p[i]);
		if (b[n-x+i] < p[i]) tmpa -= p[i]-b[n-x+i];
	}
//	printf(">> cek %d %d\n", x, tmpa);
	if (tmpa >= 0) return 1;
	else return 0;
}

int main() {
	scanf("%d %d %d", &n, &m, &a);
	for (int i = 0; i < n; i++) scanf("%d", &b[i]);
	for (int i = 0; i < m; i++) scanf("%d", &p[i]);
	sort(b, b+n);
	sort(p, p+m);

	int l = 0, r = n, mid, maks = 0;

	for (int i = 0; i < 30; i++) {
		mid = (l+r)/2;
		if (cek(mid)) {
			maks = max(maks, mid);
			l = mid+1;
		}
		else {
			r = mid-1;
		}
	}
//	printf(">> maks = %d\n", maks);
	if (maks == 0) {
		printf("0 0\n");
		return 0;
	}

	int personal = 0, total = 0;
	for (int i = 0; i < maks; i++) {
		total += p[i];
	}
	personal = total-a;
	if (personal < 0) personal = 0;
	printf("%d %d\n", maks, personal);

	return 0;
}

/*
3 3 3
1 1 2
3 5 6
1 0
*/


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int n,m,a,s;
vector<int> bike,boy;
bool check(int k){
    int i,x=a;
    s=0;
    for(i=0;i<k;i++){
        x-=max(bike[i]-boy[n-k+i],0);
        s+=min(boy[n-k+i],bike[i]);
    }
    if(x>=0){
        s=max(s-x,0);
        return true;
    }
    return false;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m>>a;
    int x,i;
    for(i=0;i<n;i++){
        cin>>x;
        boy.push_back(x);
    }
    for(i=0;i<m;i++){
        cin>>x;
        bike.push_back(x);
    }
    sort(bike.begin(),bike.end());
    sort(boy.begin(),boy.end());
    int l=0,r=min(n,m);
    //check(4);
   while(l!=r){
        int mid=(l+r)/2;
        if(check(mid))
            l=mid;
        else
            r=mid-1;
        if(r-l==1){
            if(check(r))
                l=r;
            else
                r=l;
        }
    }
    check(l);
    cout<<l<<" "<<s;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int n,m,a,s;
vector<int> bike,boy;
bool check(int k){
    int i,x=a;
    s=0;
    for(i=0;i<k;i++){
        if(bike[i]>boy[n-k+i]){
            x-=bike[i]-boy[n-k+i];
            s+=boy[n-k+i];
        }
        else
            s+=bike[i];
    }
    if(x>=0){
        s=max(s-x,0);
        return true;
    }
    return false;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m>>a;
    int x,i;
    for(i=0;i<n;i++){
        cin>>x;
        boy.push_back(x);
    }
    for(i=0;i<m;i++){
        cin>>x;
        bike.push_back(x);
    }
    sort(bike.begin(),bike.end());
    sort(boy.begin(),boy.end());
    int l=0,r=min(n,m);
    while(l!=r){
        int mid=(l+r)/2;
        if(check(mid))
            l=mid;
        else
            r=mid-1;
        if(r-l==1){
            if(check(r))
                l=r;
            else
                r=l;
        }
    }
    check(l);
    cout<<l<<" "<<s;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,m,a,s;
vector<ll> bike,boy;
bool check(ll k){
    ll i,x=a;
    s=0;
    for(i=0;i<k;i++){
        if(bike[i]>boy[n-k+i]){
            x-=bike[i]-boy[n-k+i];
            s+=boy[n-k+i];
        }
        else
            s+=bike[i];
    }
    if(x>=0){
        s=max(s-x,0LL);
        return true;
    }
    return false;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m>>a;
    ll x,i;
    for(i=0;i<n;i++){
        cin>>x;
        boy.push_back(x);
    }
    for(i=0;i<m;i++){
        cin>>x;
        bike.push_back(x);
    }
    sort(bike.begin(),bike.end());
    sort(boy.begin(),boy.end());
    ll l=0,r=min(n,m);
    while(l!=r){
        int mid=(l+r)/2;
        if(check(mid))
            l=mid;
        else
            r=mid-1;
        if(r-l==1){
            if(check(r))
                l=r;
            else
                r=l;
        }
    }
    check(l);
    cout<<l<<" "<<s;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
multiset<int> bike;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m,i,x,r=0;
    ll a;
    cin>>n>>m>>a;
    vector<int> boy(n),temp(n,-1);
    vector<pair<int,int> > aux;
    for(i=0;i<n;i++){
        cin>>boy[i];

    }
    for(i=0;i<m;i++){
         cin>>x;
         bike.insert(x);
    }
    sort(boy.begin(),boy.end());
    multiset<int> ::iterator its;
    ll s=0;
    for(i=n-1;i>=0;i--){
        its=bike.lower_bound(boy[i]);
        if(its!=bike.end()){
            aux.push_back(make_pair(*its-boy[i],boy[i]));
            bike.erase(its);
        }
        else if(bike.size()==0)
            break;
        else
            temp[i]=2;
    }
    sort(aux.begin(),aux.end());
    ll sum=0,k=0;
    for(i=0;i<aux.size();i++){
        if(sum+aux[i].first<=a){
            sum+=aux[i].first;
            s+=aux[i].second;
            k++;
        }
        else{
            bike.insert(aux[i].first+aux[i].second);
        }
    }
    //cout<<s<<" "<<k<<endl;
    for(i=0;i<n;i++){
        if(temp[i]==2&&bike.size()!=0&&boy[i]>=*(bike.begin())){
            s+=*(bike.begin());
            r++;
            bike.erase(bike.begin());
        }
    }
    // cout<<s<<" "<<r<<endl;
    s=max(s-(a-sum),0LL);
    cout<<k+r<<" "<<s;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int n,m,a,s;
vector<int> bike,boy;
bool check(int k){
    int i,x=a;
    s=0;
    for(i=0;i<k;i++){
        x-=bike[i]-boy[n-k+i];
        s+=boy[n-k+i];
    }
    if(x>=0){
        s=max(s-x,0);
        return true;
    }
    return false;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m>>a;
    int x,i;
    for(i=0;i<n;i++){
        cin>>x;
        boy.push_back(x);
    }
    for(i=0;i<m;i++){
        cin>>x;
        bike.push_back(x);
    }
    sort(bike.begin(),bike.end());
    sort(boy.begin(),boy.end());
    int l=0,r=min(n,m);
    while(l!=r){
        int mid=(l+r)/2;
        if(check(mid))
            l=mid;
        else
            r=mid-1;
        if(r-l==1){
            if(check(r))
                l=r;
            else
                r=l;
        }
    }
    check(l);
    cout<<l<<" "<<s;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+20;
int n,m,a,p[N],b[N];
int main()
{
	scanf("%d%d%d",&n,&m,&a);
	for(int i=1;i<=n;i++)
		scanf("%d",&b[i]);
	for(int i=1;i<=m;i++)
		scanf("%d",&p[i]);
	sort(b+1,b+1+n);
	sort(p+1,p+1+m);
/*
	cout<<endl;
	for(int i=1;i<=n;i++)
		cout<<b[i]<<' ';
	cout<<endl;
	for(int i=1;i<=m;i++)
		cout<<p[i]<<' ';
	cout<<endl;
*/
	int rg=1,cnt=0,ans=0;
	for(int i=max(1,n-m+1);i<=n;i++)
	{
		if(b[i]+a>=p[rg])
		{
			if(b[i]<p[rg])
				a=a-(p[rg]-b[i]),cnt+=b[i];
			else
				cnt+=p[rg];
			ans++,rg++;
		}
		if(rg>m)
			break;
	}
	if(a>0)
		cnt-=a;
	cnt=max(0,cnt);
	cout<<ans<<' '<<cnt<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+20;
int n,m,a,p[N],b[N];
int main()
{
	scanf("%d%d%d",&n,&m,&a);
	for(int i=1;i<=n;i++)
		scanf("%d",&b[i]);
	for(int i=1;i<=m;i++)
		scanf("%d",&p[i]);
	sort(b+1,b+1+n);
	sort(p+1,p+1+m);
	int rg=1,cnt=0,ans=0;
	for(int i=1;i<=n;i++)
	{
		if(b[i]+a>=p[rg])
		{
			if(b[i]<p[rg])
				a=a-(p[rg]-b[i]),cnt+=b[i];
			else
				cnt+=p[rg];
			ans++,rg++;
		}
		if(rg>m)
			break;
	}
	if(a>0)
		cnt-=a;
	cout<<ans<<' '<<cnt<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+20;
int n,m,a,p[N],b[N];
bool check(int pos)
{
	int rg=1,ans=0;
	for(int i=max(1,n-pos+1);i<=n;i++)
	{
		if(b[i]+a>=p[rg])
			ans++,rg++;
		if(rg>m)
			break;
	}
	return ans>=pos;
}
int main()
{
	scanf("%d%d%d",&n,&m,&a);
	for(int i=1;i<=n;i++)
		scanf("%d",&b[i]);
	for(int i=1;i<=m;i++)
		scanf("%d",&p[i]);
	sort(b+1,b+1+n);
	sort(p+1,p+1+m);
/*
	cout<<endl;
	for(int i=1;i<=n;i++)
		cout<<b[i]<<' ';
	cout<<endl;
	for(int i=1;i<=m;i++)
		cout<<p[i]<<' ';
	cout<<endl;
*/
	int l=1,r=n;
	while(l<=r)
	{
		int m=l+r>>1;
		if(check(m))
			l=m+1;
		else
			r=m-1;
	}
	int pos=l-1;
	int rg=1,cnt=0,ans=0;
	for(int i=max(1,n-pos+1);i<=n;i++)
	{
		if(b[i]+a>=p[rg])
		{
			if(b[i]<p[rg])
				a=a-(p[rg]-b[i]),cnt+=b[i];
			else
				cnt+=p[rg];
			ans++,rg++;
		}
		if(rg>m)
			break;
	}
	if(a>0)
		cnt-=a;
	cnt=max(0,cnt);
	cout<<ans<<' '<<cnt<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+20;
int n,m,a,p[N],b[N];
int main()
{
	scanf("%d%d%d",&n,&m,&a);
	for(int i=1;i<=n;i++)
		scanf("%d",&b[i]);
	for(int i=1;i<=m;i++)
		scanf("%d",&p[i]);
	sort(b+1,b+1+n);
	sort(p+1,p+1+m);
	int rg=1,cnt=0,ans=0;
	for(int i=1;i<=n;i++)
	{
		if(b[i]+a>=p[rg])
		{
			if(b[i]<p[rg])
				a=a-(p[rg]-b[i]),cnt+=b[i];
			else
				cnt+=p[rg];
			ans++,rg++;
		}
		if(rg>m)
			break;
	}
	if(a>0)
		cnt-=a;
	cnt=max(0,cnt);
	cout<<ans<<' '<<cnt<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+20;
int n,m,a,p[N],b[N];
bool check(int pos,int a)
{
	int rg=1,ans=0;
	for(int i=max(1,n-pos+1);i<=n;i++)
	{
		if(b[i]+a>=p[rg])
		{
			if(b[i]<p[rg])
				a=a-(p[rg]-b[i]);
			ans++,rg++;
		}
		if(rg>m)
			break;
	}
	return ans>=pos;
}
int main()
{
	scanf("%d%d%d",&n,&m,&a);
	for(int i=1;i<=n;i++)
		scanf("%d",&b[i]);
	for(int i=1;i<=m;i++)
		scanf("%d",&p[i]);
	sort(b+1,b+1+n);
	sort(p+1,p+1+m);
/*
	cout<<endl;
	for(int i=1;i<=n;i++)
		cout<<b[i]<<' ';
	cout<<endl;
	for(int i=1;i<=m;i++)
		cout<<p[i]<<' ';
	cout<<endl;
*/
	int l=0,r=min(n,m),pos=0;
	while(l<=r)
	{
		int m=l+r>>1;
		if(check(m,a))
			l=m+1,pos=m;
		else
			r=m-1;
	}
	int rg=1,cnt=0,ans=0;
	for(int i=max(1,n-pos+1);i<=n;i++)
	{
		if(b[i]+a>=p[rg])
		{
			if(b[i]<p[rg])
				a=a-(p[rg]-b[i]),cnt+=b[i];
			else
				cnt+=p[rg];
			ans++,rg++;
		}
		if(rg>m)
			break;
	}
	if(a>0)
		cnt-=a;
	cnt=max(0,cnt);
	cout<<ans<<' '<<cnt<<endl;
	return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
double  EPS= 1e-9;
#define MAXN 200000 + 6

int conversion(string p){ int o; o=atoi(p.c_str()); return o; }
string toString(int h){ stringstream ss; ss<<h; return ss.str(); }
long long gcd(long long a,long long b) {return (b==0 ? a : gcd(b,a%b));}
long long lcm(long long a,long long b) {return (a*(b/gcd(a,b))); }
long long toi(string p){long long x;istringstream in(p); in>>x; return x;}
long long square(long n) { return n*n; }
long long fastexp(long base,long power) {if (power==0) return 1;else if (power%2==0) return square(fastexp(base,power/2));else return base * (fastexp(base,power-1));}
bool is_prime(int n) {for (int i=2; i*i<=n; i++) if (n%i == 0) return 0;return 1;}
int logba(int a,int b){return log(a)/log(b);}
int rd(double d){int x=(int)((double)d + 0.5);return x;}
// IMPRMIR DECIMALES -> %.3f
// Logaritmo en cualquir base ( nota: solo cambiar el 2 ) printf("Case %d: %.0lf\n", cnt++, max(0.0,ceil(log(n) / log(2))));

vector<ll> M,N;

ll can(ll m,ll a)
{
    ll sum=0,res=0;
    int ind=0;
    for(int i=(M.size()-m);i<M.size()&&ind<N.size();i++)
    {
        if(M[i]>=N[ind])
        {
            sum+=N[ind];
            res++;
        }
        else if(M[i]+a>=N[ind])
        {
            sum+=M[i];
            a-=abs(M[i]-N[ind]);
            res++;
        }
        ind++;
    }
    return res;
}

int main(){

     #ifndef ONLINE_JUDGE
     freopen("D:/input.txt","r",stdin);
     //freopen("D:/output.txt","w",stdout);
     #endif

    ll n,m,a,x,h,l,a1,v;
    cin>>n>>m>>a;
    a1=a;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        M.push_back(x);
    }
    for(int i=0;i<m;i++)
    {
        cin>>x;
        N.push_back(x);
    }
    sort(M.begin(),M.end());
    sort(N.begin(),N.end());
    l=0,h=n+1;
    while(h-l>1)
    {
        ll m=(h+l)/2;
        if(can(m,a)==m) l=m;
        else h=m;
    }
    if(can(l,a)==l) v=l;
    else v=h;
    ll sum=0,res=0;
    int ind=0;
    for(int i=n-v;i<n&&ind<N.size();i++)
    {
        if(M[i]>=N[ind])
        {
            sum+=N[ind];
            res++;
        }
        else if(M[i]+a>=N[ind])
        {
            sum+=M[i];
            a-=abs(M[i]-N[ind]);
            res++;
        }
        ind++;
    }
    if(a>0) sum-=a;
    cout<<res<<" "<<sum<<endl;
}


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

typedef long long ll;
typedef unsigned long long ull;
double  EPS= 1e-9;
#define MAXN 200000 + 6

int conversion(string p){ int o; o=atoi(p.c_str()); return o; }
string toString(int h){ stringstream ss; ss<<h; return ss.str(); }
long long gcd(long long a,long long b) {return (b==0 ? a : gcd(b,a%b));}
long long lcm(long long a,long long b) {return (a*(b/gcd(a,b))); }
long long toi(string p){long long x;istringstream in(p); in>>x; return x;}
long long square(long n) { return n*n; }
long long fastexp(long base,long power) {if (power==0) return 1;else if (power%2==0) return square(fastexp(base,power/2));else return base * (fastexp(base,power-1));}
bool is_prime(int n) {for (int i=2; i*i<=n; i++) if (n%i == 0) return 0;return 1;}
int logba(int a,int b){return log(a)/log(b);}
// IMPRMIR DECIMALES -> %.3f
// Logaritmo en cualquir base ( nota: solo cambiar el 2 ) printf("Case %d: %.0lf\n", cnt++, max(0.0,ceil(log(n) / log(2))));

int main()
{

    #ifndef ONLINE_JUDGE
    freopen("D:/input.txt","r",stdin);
    //freopen("D:/output.txt","w",stdout);
    #endif

    int n,m,a,x,y,res=0,sum=0,ind=0,t=0;
    cin>>n>>m>>a;
    vector< int > M,N,P;
    map< int , int > L;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        L[x]++;
        M.push_back(x);
    }
    for(int i=0;i<m;i++)
    {
        cin>>x;
        P.push_back(x);
    }
    sort(M.begin(),M.end());
    sort(P.begin(),P.end());
    for(int i=0;i<n;i++) t+=P[i];

    if(a>=t) cout<<n<<" "<<0<<endl;
    else
    {
        for(int i=0;i<M.size();i++)
        {
            if(M[i]>=P[ind])
            {
                sum+=P[ind];
                res++;
                ind++;
            }
            else if(a+M[i]>=P[ind])
            {
                sum+=abs(M[i]);
                a-=abs(M[i]-P[ind]);
                res++;
                ind++;
            }
        }
        if(a>0) sum-=a;
        cout<<res<<" "<<sum<<endl;
    }
}


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

typedef long long ll;
typedef unsigned long long ull;
double  EPS= 1e-9;
#define MAXN 200000 + 6

int conversion(string p){ int o; o=atoi(p.c_str()); return o; }
string toString(int h){ stringstream ss; ss<<h; return ss.str(); }
long long gcd(long long a,long long b) {return (b==0 ? a : gcd(b,a%b));}
long long lcm(long long a,long long b) {return (a*(b/gcd(a,b))); }
long long toi(string p){long long x;istringstream in(p); in>>x; return x;}
long long square(long n) { return n*n; }
long long fastexp(long base,long power) {if (power==0) return 1;else if (power%2==0) return square(fastexp(base,power/2));else return base * (fastexp(base,power-1));}
bool is_prime(int n) {for (int i=2; i*i<=n; i++) if (n%i == 0) return 0;return 1;}
int logba(int a,int b){return log(a)/log(b);}
int rd(double d){int x=(int)((double)d + 0.5);return x;}
// IMPRMIR DECIMALES -> %.3f
// Logaritmo en cualquir base ( nota: solo cambiar el 2 ) printf("Case %d: %.0lf\n", cnt++, max(0.0,ceil(log(n) / log(2))));

vector<ll> M,N;

ll can(ll m,ll a)
{
    ll sum=0,res=0;
    int ind=0;
    for(int i=(M.size()-m);i<M.size()&&ind<N.size();i++)
    {
        if(M[i]>=N[ind])
        {
            sum+=N[ind];
            res++;
        }
        else if(M[i]+a>=N[ind])
        {
            sum+=M[i];
            a-=abs(M[i]-N[ind]);
            res++;
        }
        ind++;
    }
    return res;
}
int main(){

     #ifndef ONLINE_JUDGE
     freopen("D:/input.txt","r",stdin);
     //freopen("D:/output.txt","w",stdout);
     #endif

    ll n,m,a,x,h,l,a1;
    cin>>n>>m>>a;
    a1=a;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        M.push_back(x);
    }
    for(int i=0;i<m;i++)
    {
        cin>>x;
        N.push_back(x);
    }
    sort(M.begin(),M.end());
    sort(N.begin(),N.end());
    l=0,h=n;
    while(h-l>1)
    {
        ll m=(h+l)/2;
        if(can(m,a)==m) l=m;
        else h=m;
    }
    ll sum=0,res=0;
    int ind=0;
    for(int i=n-h;i<n&&ind<N.size();i++)
    {
        if(M[i]>=N[ind])
        {
            sum+=N[ind];
            res++;
        }
        else if(M[i]+a>=N[ind])
        {
            sum+=M[i];
            a-=abs(M[i]-N[ind]);
            res++;
        }
        ind++;
    }
    ind=0;
    ll sum1=0,res1=0;
    for(int i=n-l;i<n&&i<N.size();i++)
    {
        if(M[i]>=N[ind])
        {
            sum1+=N[ind];
            res1++;
        }
        else if(M[i]+a1>=N[ind])
        {
            sum1+=M[i];
            a1-=abs(M[i]-N[ind]);
            res1++;
        }
        ind++;
    }
    if(res>=res1)
    {
        if(a>0) sum-=a;
        cout<<res<<" "<<sum<<endl;
    }
    else
    {
        if(a1>0) sum1-=a1;
        cout<<res1<<" "<<sum1<<endl;
    }
}


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

typedef long long ll;
typedef unsigned long long ull;
double  EPS= 1e-9;
#define MAXN 200000 + 6

int conversion(string p){ int o; o=atoi(p.c_str()); return o; }
string toString(int h){ stringstream ss; ss<<h; return ss.str(); }
long long gcd(long long a,long long b) {return (b==0 ? a : gcd(b,a%b));}
long long lcm(long long a,long long b) {return (a*(b/gcd(a,b))); }
long long toi(string p){long long x;istringstream in(p); in>>x; return x;}
long long square(long n) { return n*n; }
long long fastexp(long base,long power) {if (power==0) return 1;else if (power%2==0) return square(fastexp(base,power/2));else return base * (fastexp(base,power-1));}
bool is_prime(int n) {for (int i=2; i*i<=n; i++) if (n%i == 0) return 0;return 1;}
int logba(int a,int b){return log(a)/log(b);}
// IMPRMIR DECIMALES -> %.3f
// Logaritmo en cualquir base ( nota: solo cambiar el 2 ) printf("Case %d: %.0lf\n", cnt++, max(0.0,ceil(log(n) / log(2))));

int main()
{

    #ifndef ONLINE_JUDGE
    freopen("D:/input.txt","r",stdin);
    //freopen("D:/output.txt","w",stdout);
    #endif

    int n,m,a,x,y,res=0,sum=0,ind=0,t=0;
    cin>>n>>m>>a;
    vector< int > M,N,P;
    map< int , int > L;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        L[x]++;
        M.push_back(x);
    }
    for(int i=0;i<m;i++)
    {
        cin>>x;
        P.push_back(x);
    }
    sort(M.begin(),M.end());
    sort(P.begin(),P.end());
    for(int i=0;i<n;i++) t+=P[i];

    if(a>=t) cout<<n<<" "<<0<<endl;
    else
    {
        for(int i=0;i<M.size();i++)
        {
            if(M[i]>=P[ind])
            {
                sum+=P[ind];
                res++;
                ind++;
            }
            else if(a+M[i]>=P[ind])
            {
                sum+=abs(M[i]);
                a-=abs(M[i]-P[ind]);
                res++;
                ind++;
            }
            if(ind==m) break;
        }
        if(a>0) sum-=a;
        cout<<res<<" "<<sum<<endl;
    }
}


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

typedef long long ll;
typedef unsigned long long ull;
double  EPS= 1e-9;
#define MAXN 200000 + 6

int conversion(string p){ int o; o=atoi(p.c_str()); return o; }
string toString(int h){ stringstream ss; ss<<h; return ss.str(); }
long long gcd(long long a,long long b) {return (b==0 ? a : gcd(b,a%b));}
long long lcm(long long a,long long b) {return (a*(b/gcd(a,b))); }
long long toi(string p){long long x;istringstream in(p); in>>x; return x;}
long long square(long n) { return n*n; }
long long fastexp(long base,long power) {if (power==0) return 1;else if (power%2==0) return square(fastexp(base,power/2));else return base * (fastexp(base,power-1));}
bool is_prime(int n) {for (int i=2; i*i<=n; i++) if (n%i == 0) return 0;return 1;}
int logba(int a,int b){return log(a)/log(b);}
int rd(double d){int x=(int)((double)d + 0.5);return x;}
// IMPRMIR DECIMALES -> %.3f
// Logaritmo en cualquir base ( nota: solo cambiar el 2 ) printf("Case %d: %.0lf\n", cnt++, max(0.0,ceil(log(n) / log(2))));

vector<ll> M,N;

ll can(ll m,ll a)
{
    ll sum=0,res=0;
    int ind=0;
    for(int i=(M.size()-m);i<M.size()&&ind<N.size();i++)
    {
        if(M[i]>=N[ind])
        {
            sum+=N[ind];
            res++;
        }
        else if(M[i]+a>=N[ind])
        {
            sum+=M[i];
            a-=abs(M[i]-N[ind]);
            res++;
        }
        ind++;
    }
    return res;
}

int main(){

     #ifndef ONLINE_JUDGE
     freopen("D:/input.txt","r",stdin);
     //freopen("D:/output.txt","w",stdout);
     #endif

    ll n,m,a,x,h,l,a1,v;
    cin>>n>>m>>a;
    a1=a;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        M.push_back(x);
    }
    for(int i=0;i<m;i++)
    {
        cin>>x;
        N.push_back(x);
    }
    sort(M.begin(),M.end());
    sort(N.begin(),N.end());
    l=0,h=n+1;
    while(h-l>1)
    {
        ll m=(h+l)/2;
        if(can(m,a)==m) l=m;
        else h=m;
    }
    if(can(l,a)==l) v=l;
    else v=h;
    ll sum=0,res=0;
    int ind=0;
    for(int i=n-v;i<n&&ind<N.size();i++)
    {
        if(M[i]>=N[ind])
        {
            sum+=N[ind];
            res++;
        }
        else if(M[i]+a>=N[ind])
        {
            sum+=M[i];
            a-=abs(M[i]-N[ind]);
            res++;
        }
        ind++;
    }
    if(a>0)
    {
        if(a>sum) sum=0;
        else sum-=a;
    }
    cout<<res<<" "<<sum<<endl;
}


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

typedef long long ll;
typedef unsigned long long ull;
double  EPS= 1e-9;
#define MAXN 200000 + 6

int conversion(string p){ int o; o=atoi(p.c_str()); return o; }
string toString(int h){ stringstream ss; ss<<h; return ss.str(); }
long long gcd(long long a,long long b) {return (b==0 ? a : gcd(b,a%b));}
long long lcm(long long a,long long b) {return (a*(b/gcd(a,b))); }
long long toi(string p){long long x;istringstream in(p); in>>x; return x;}
long long square(long n) { return n*n; }
long long fastexp(long base,long power) {if (power==0) return 1;else if (power%2==0) return square(fastexp(base,power/2));else return base * (fastexp(base,power-1));}
bool is_prime(int n) {for (int i=2; i*i<=n; i++) if (n%i == 0) return 0;return 1;}
int logba(int a,int b){return log(a)/log(b);}
// IMPRMIR DECIMALES -> %.3f
// Logaritmo en cualquir base ( nota: solo cambiar el 2 ) printf("Case %d: %.0lf\n", cnt++, max(0.0,ceil(log(n) / log(2))));

int main()
{

    #ifndef ONLINE_JUDGE
    freopen("D:/input.txt","r",stdin);
    //freopen("D:/output.txt","w",stdout);
    #endif

    int n,m,a,x,y,res=0,sum=0,ind=0,t=0;
    cin>>n>>m>>a;
    vector< int > M,N,P;
    map< int , int > L;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        L[x]++;
        M.push_back(x);
    }
    for(int i=0;i<m;i++)
    {
        cin>>x;
        P.push_back(x);
    }
    sort(M.begin(),M.end());
    sort(P.begin(),P.end());
    for(int i=0;i<n;i++) t+=P[i];

    if(a>=t) cout<<0<<" "<<0<<endl;
    else
    {
        for(int i=0;i<M.size();i++)
        {
            if(M[i]>=P[ind])
            {
                sum+=P[ind];
                res++;
                ind++;
            }
            else if(a+M[i]>=P[ind])
            {
                sum+=abs(M[i]);
                a-=abs(M[i]-P[ind]);
                res++;
                ind++;
            }
        }
        if(a>0) sum-=a;
        cout<<res<<" "<<sum<<endl;
    }
}


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

typedef long long ll;
typedef unsigned long long ull;
double  EPS= 1e-9;
#define MAXN 200000 + 6

int conversion(string p){ int o; o=atoi(p.c_str()); return o; }
string toString(int h){ stringstream ss; ss<<h; return ss.str(); }
long long gcd(long long a,long long b) {return (b==0 ? a : gcd(b,a%b));}
long long lcm(long long a,long long b) {return (a*(b/gcd(a,b))); }
long long toi(string p){long long x;istringstream in(p); in>>x; return x;}
long long square(long n) { return n*n; }
long long fastexp(long base,long power) {if (power==0) return 1;else if (power%2==0) return square(fastexp(base,power/2));else return base * (fastexp(base,power-1));}
bool is_prime(int n) {for (int i=2; i*i<=n; i++) if (n%i == 0) return 0;return 1;}
int logba(int a,int b){return log(a)/log(b);}
// IMPRMIR DECIMALES -> %.3f
// Logaritmo en cualquir base ( nota: solo cambiar el 2 ) printf("Case %d: %.0lf\n", cnt++, max(0.0,ceil(log(n) / log(2))));

vector< int > M,N,P;
map< int , int > L;
int n,m,a,x,y,res=0,sum=0,ind=0,t=0;

int main()
{

    cin>>n>>m>>a;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        L[x]++;
        M.push_back(x);
    }
    for(int i=0;i<m;i++)
    {
        cin>>x;
        P.push_back(x);
    }
    sort(M.begin(),M.end());
    sort(P.begin(),P.end());
    for(int i=0;i<n;i++) t+=P[i];

    if(a>=t) cout<<n<<" "<<0<<endl;
    else
    {
        for(int i=0;i<n&&ind<m;i++)
        {
            if(M[i]>=P[ind])
            {
                sum+=P[ind];
                res++;
                ind++;
            }
            else if(a+M[i]>=P[ind])
            {
                sum+=abs(M[i]);
                a-=abs(M[i]-P[ind]);
                res++;
                ind++;
            }
        }
        if(a>0) sum-=a;
        cout<<res<<" "<<sum<<endl;
    }
}


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

typedef long long ll;
typedef unsigned long long ull;
double  EPS= 1e-9;
#define MAXN 200000 + 6

int conversion(string p){ int o; o=atoi(p.c_str()); return o; }
string toString(int h){ stringstream ss; ss<<h; return ss.str(); }
long long gcd(long long a,long long b) {return (b==0 ? a : gcd(b,a%b));}
long long lcm(long long a,long long b) {return (a*(b/gcd(a,b))); }
long long toi(string p){long long x;istringstream in(p); in>>x; return x;}
long long square(long n) { return n*n; }
long long fastexp(long base,long power) {if (power==0) return 1;else if (power%2==0) return square(fastexp(base,power/2));else return base * (fastexp(base,power-1));}
bool is_prime(int n) {for (int i=2; i*i<=n; i++) if (n%i == 0) return 0;return 1;}
int logba(int a,int b){return log(a)/log(b);}
// IMPRMIR DECIMALES -> %.3f
// Logaritmo en cualquir base ( nota: solo cambiar el 2 ) printf("Case %d: %.0lf\n", cnt++, max(0.0,ceil(log(n) / log(2))));

int main()
{

    #ifndef ONLINE_JUDGE
    freopen("D:/input.txt","r",stdin);
    //freopen("D:/output.txt","w",stdout);
    #endif

    int n,m,a,x,y,res=0,sum=0,ind=0,t=0;
    cin>>n>>m>>a;
    vector< int > M,N,P;
    map< int , int > L;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        L[x]++;
        M.push_back(x);
    }
    for(int i=0;i<m;i++)
    {
        cin>>x;
        P.push_back(x);
    }
    sort(M.begin(),M.end());
    sort(P.begin(),P.end());
    for(int i=0;i<n;i++) t+=P[i];

    if(a>=t) cout<<n<<" "<<0<<endl;
    else
    {
        for(int i=0;i<M.size()&&ind<m;i++)
        {
            if(M[i]>=P[ind])
            {
                sum+=P[ind];
                res++;
                ind++;
            }
            else if(a+M[i]>=P[ind])
            {
                sum+=abs(M[i]);
                a-=abs(M[i]-P[ind]);
                res++;
                ind++;
            }
        }
        if(a>0) sum-=a;
        cout<<res<<" "<<sum<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
const int mxmx=1e5+10;

#define x first
#define y second
#define mp make_pair
long long n,m;
long long a;
//   shakhsi bycle
long long b[mxmx],p[mxmx];
long long l,r,mid;
long long ANS;
long long check(long long t,bool A){
	if(A==0){
		long long jam=a;
		long long i=0;
		long long j=n-t;
		if(j<0)return 0;
		for(;i<t;i++,j++){
			if(p[i]-b[j]>=0){
				jam-=(p[i]-b[j]);
				if(jam<0)return 0;
			}
		}
		return 1;
	}
	else{
		long long jam=0;
		long long i=0;
		long long j=n-t;
		for(;i<t;i++,j++){
			if(p[i]>=b[j]){
				a-=(p[i]-b[j]);
				jam+=b[j];
			}
			else{
				jam+=p[i];
			}
		}
		return jam;
	}
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>n>>m>>a;
	for(long long i=0;i<n;i++){
		cin>>b[i];
	}
	for(long long i=0;i<m;i++){
		cin>>p[i];
	}
	sort(b,b+n);
	sort(p,p+m);
	l=0;
	r=min(n,m)+1;
	while(l<r){
		mid=(l+r)/2;
		if(check(mid,0)==1){
			l=mid+1;
		}
		else{
			r=mid;
		}
	}
	ANS=r-1;
	cout<<ANS<<" ";
	ANS=check(ANS,1)-a;
	if(ANS>=0)cout<<ANS;
	else cout<<0;
}


#include<bits/stdc++.h>
using namespace std;
const int mxmx=1e5+10;

#define x first
#define y second
#define mp make_pair
typedef long long ll;

int n,m;
ll a;
//   shakhsi bycle
int b[mxmx],p[mxmx];
int l,r,mid;
int ANS;
ll check(int t,bool A){
	if(A==0){
		ll jam=a;
		int i=0;
		int j=n-t;
		if(j<0)return 0;
		for(;i<t;i++,j++){
			if(p[i]-b[j]>=0){
				jam-=(p[i]-b[j]);
				if(jam<0)return 0;
			}
		}
		return 1;
	}
	else{
		ll jam=0;
		int i=0;
		int j=n-t;
		for(;i<t;i++,j++){
			if(p[i]>=b[j]){
				a-=(p[i]-b[j]);
				jam+=b[j];
			}
			else{
				jam+=p[i];
			}
		}
		return jam;
	}
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>n>>m>>a;
	for(int i=0;i<n;i++){
		cin>>b[i];
	}
	for(int i=0;i<m;i++){
		cin>>p[i];
	}
	sort(b,b+n);
	sort(p,p+m);
	l=0;
	r=n+1;
	while(l<r){
		mid=(l+r)/2;
		if(check(mid,0)==1){
			l=mid+1;
		}
		else{
			r=mid;
		}
	}
	ANS=r-1;
	cout<<ANS<<" ";
	cout<<check(ANS,1)-a;
}


#include<bits/stdc++.h>
using namespace std;
const int mxmx=1e5+10;

#define x first
#define y second
#define mp make_pair
long long n,m;
long long a;
//   shakhsi bycle
long long b[mxmx],p[mxmx];
long long l,r,mid;
long long ANS;
long long check(long long t,bool A){
	if(A==0){
		long long jam=a;
		long long i=0;
		long long j=n-t;
		if(j<0)return 0;
		for(;i<t;i++,j++){
			if(p[i]-b[j]>=0){
				jam-=(p[i]-b[j]);
				if(jam<0)return 0;
			}
		}
		return 1;
	}
	else{
		long long jam=0;
		long long i=0;
		long long j=n-t;
		for(;i<t;i++,j++){
			if(p[i]>=b[j]){
				a-=(p[i]-b[j]);
				jam+=b[j];
			}
			else{
				jam+=p[i];
			}
		}
		return jam;
	}
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>n>>m>>a;
	for(long long i=0;i<n;i++){
		cin>>b[i];
	}
	for(long long i=0;i<m;i++){
		cin>>p[i];
	}
	sort(b,b+n);
	sort(p,p+m);
	l=0;
	r=min(n,m)+1;
	while(l<r){
		mid=(l+r)/2;
		if(check(mid,0)==1){
			l=mid+1;
		}
		else{
			r=mid;
		}
	}
	ANS=r-1;
	cout<<ANS<<" ";
	if(check(ANS,1)-a>=0)cout<<check(ANS,1)-a;
	else cout<<0;
}


#include<bits/stdc++.h>
using namespace std;
const int mxmx=1e5+10;

#define x first
#define y second
#define mp make_pair
long long n,m;
long long a;
//   shakhsi bycle
long long b[mxmx],p[mxmx];
long long l,r,mid;
long long ANS;
long long check(long long t,bool A){
	if(A==0){
		long long jam=a;
		long long i=0;
		long long j=n-t;
		if(j<0)return 0;
		for(;i<t;i++,j++){
			if(p[i]-b[j]>=0){
				jam-=(p[i]-b[j]);
				if(jam<0)return 0;
			}
		}
		return 1;
	}
	else{
		long long jam=0;
		long long i=0;
		long long j=n-t;
		for(;i<t;i++,j++){
			if(p[i]>=b[j]){
				a-=(p[i]-b[j]);
				jam+=b[j];
			}
			else{
				jam+=p[i];
			}
		}
		return jam;
	}
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>n>>m>>a;
	for(long long i=0;i<n;i++){
		cin>>b[i];
	}
	for(long long i=0;i<m;i++){
		cin>>p[i];
	}
	sort(b,b+n);
	sort(p,p+m);
	l=0;
	r=min(n,m)+1;
	while(l<r){
		mid=(l+r)/2;
		if(check(mid,0)==1){
			l=mid+1;
		}
		else{
			r=mid;
		}
	}
	ANS=r-1;
	cout<<ANS<<" ";
	cout<<check(ANS,1)-a;
}


#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(), (c).end()
#define cnt(c, x) ((c).find(x) != (c).end())
#define pb push_back
#define FOR(i, a, n) for(int i = (a); i < (n); i++)
#define REP(i, n) for(int i = 0; i < (n); i++)
#define SZ(x) ((int) (x).size())
#define mp(x,y) make_pair((x), (y))
#define mp3(x,y,z) make_pair((x), make_pair( (y), (z)))
#define foreach(C, i) for(auto i = (C).begin(); i != (C).end(); i++)
#define xx first
#define yy second
#define clr clear()
#define var(x) cout<< #x << " = "<<x<<"\n";
#define print(x) for_each((x).begin(), (x).end(), [](auto n) { cout<<x<<" " })
typedef int32_t i3;
typedef int64_t i6;
typedef vector<i3> vi;
typedef pair<i3,i3> ii;
typedef vector<pair<i3,i3> > vii;

int getCost(vector<int>& money, vector<int>& bike, int a, int cnt)
{
	int n = SZ(money),cst = 0, j = 0;
	for(int i = n - cnt; i < n; i++)
	{
		if (money[i] >= bike[j])
		{
			cst += bike[j];
			cnt--;
		}
		else if (money[i] + a >= bike[j])
		{
			cst += money[i];
			a -= (bike[j] - money[i]);
			cnt--;
		}
		else
			break;
		j++;
	}
	if (a > 0)
		cst = max(0, cst - a);
	if (cnt == 0)
		return cst;
	else
		return -1;
}

int main()
{
	int n,m,a; cin >> n >> m >> a;
	vector<int> money(n), bike(m);
	REP(i,n)
		cin >> money[i];
	REP(i,m)
		cin >> bike[i];
	sort(all(money)); sort(all(bike));
	int lo = 1, hi = min(n,m);
	pair<int,int> ans = {0,0};
	while (lo <= hi)
	{
		int mid = (lo + hi)/2;
		int cst = getCost(money, bike, a, mid);
		if (cst == -1)
			hi = mid - 1;
		else
		{
			ans = make_pair(mid, cst);
			lo = mid + 1;
		}
	}
	cout << ans.first << " " << ans.second << "\n";
	return (0);
}


#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(), (c).end()
#define cnt(c, x) ((c).find(x) != (c).end())
#define pb push_back
#define FOR(i, a, n) for(int i = (a); i < (n); i++)
#define REP(i, n) for(int i = 0; i < (n); i++)
#define SZ(x) ((int) (x).size())
#define mp(x,y) make_pair((x), (y))
#define mp3(x,y,z) make_pair((x), make_pair( (y), (z)))
#define foreach(C, i) for(auto i = (C).begin(); i != (C).end(); i++)
#define xx first
#define yy second
#define clr clear()
#define var(x) cout<< #x << " = "<<x<<"\n";
#define print(x) for_each((x).begin(), (x).end(), [](auto n) { cout<<x<<" " })
typedef int32_t i3;
typedef int64_t i6;
typedef vector<i3> vi;
typedef pair<i3,i3> ii;
typedef vector<pair<i3,i3> > vii;

int main()
{
	int n,m,a; cin >> n >> m >> a;
	vector<int> money(n), bike(m);
	REP(i,n)
		cin >> money[i];
	REP(i,m)
		cin >> bike[i];
	sort(all(money)); sort(all(bike));
	int ptr1 = 0, ptr2 = 0, personal = 0, cnt = 0;
	while (ptr1 < SZ(money) && ptr2 < SZ(bike))
	{
		if (money[ptr1] >= bike[ptr2])
		{
			personal += bike[ptr2];
			ptr1++; ptr2++; cnt++;
		}
		else if (money[ptr1] + a >= bike[ptr2])
		{
			personal += money[ptr1];
			a -= (bike[ptr2] - money[ptr1]);
			ptr1++; ptr2++; cnt++;
		}
		else
		{
			ptr1++;
		}
	}
	personal -= a;
	cout << cnt << " " << personal << "\n";
}


#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(), (c).end()
#define cnt(c, x) ((c).find(x) != (c).end())
#define pb push_back
#define FOR(i, a, n) for(int i = (a); i < (n); i++)
#define REP(i, n) for(int i = 0; i < (n); i++)
#define SZ(x) ((int) (x).size())
#define mp(x,y) make_pair((x), (y))
#define mp3(x,y,z) make_pair((x), make_pair( (y), (z)))
#define foreach(C, i) for(auto i = (C).begin(); i != (C).end(); i++)
#define xx first
#define yy second
#define clr clear()
#define var(x) cout<< #x << " = "<<x<<"\n";
#define print(x) for_each((x).begin(), (x).end(), [](auto n) { cout<<x<<" " })
typedef int32_t i3;
typedef int64_t i6;
typedef vector<i3> vi;
typedef pair<i3,i3> ii;
typedef vector<pair<i3,i3> > vii;

int getCost(vector<int>& money, vector<int>& bike, int a, int cnt)
{
	int n = SZ(money),cst = 0, j = 0;
	for(int i = n - cnt; i < n; i++)
	{
		if (money[i] >= bike[j])
		{
			cst += bike[j];
			cnt--;
		}
		else if (money[i] + a >= bike[j])
		{
			cst += money[i];
			a -= (bike[j] - money[i]);
			cnt--;
		}
		else
			break;
		j++;
	}
	if (a > 0)
		cst = max(0, cst - a);
	if (cnt == 0)
		return cst;
	else
		return -1;
}

int main()
{
	int n,m,a; cin >> n >> m >> a;
	vector<int> money(n), bike(m);
	REP(i,n)
		cin >> money[i];
	REP(i,m)
		cin >> bike[i];
	sort(all(money)); sort(all(bike));
	int lo = 1, hi = n;
	pair<int,int> ans = {0,0};
	while (lo <= hi)
	{
		int mid = (lo + hi)/2;
		int cst = getCost(money, bike, a, mid);
		if (cst == -1)
			hi = mid - 1;
		else
		{
			ans = make_pair(mid, cst);
			lo = mid + 1;
		}
	}
	cout << ans.first << " " << ans.second << "\n";
	return (0);
}


#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(), (c).end()
#define cnt(c, x) ((c).find(x) != (c).end())
#define pb push_back
#define FOR(i, a, n) for(int i = (a); i < (n); i++)
#define REP(i, n) for(int i = 0; i < (n); i++)
#define SZ(x) ((int) (x).size())
#define mp(x,y) make_pair((x), (y))
#define mp3(x,y,z) make_pair((x), make_pair( (y), (z)))
#define foreach(C, i) for(auto i = (C).begin(); i != (C).end(); i++)
#define xx first
#define yy second
#define clr clear()
#define var(x) cout<< #x << " = "<<x<<"\n";
#define print(x) for_each((x).begin(), (x).end(), [](auto n) { cout<<x<<" " })
typedef int32_t i3;
typedef int64_t i6;
typedef vector<i3> vi;
typedef pair<i3,i3> ii;
typedef vector<pair<i3,i3> > vii;

int main()
{
	int n,m,a; cin >> n >> m >> a;
	vector<int> money(n), bike(m);
	REP(i,n)
		cin >> money[i];
	REP(i,m)
		cin >> bike[i];
	sort(all(money)); sort(all(bike));
	int ptr1 = 0, ptr2 = 0, personal = 0, cnt = 0;
	while (ptr1 < SZ(money) && ptr2 < SZ(bike))
	{
		if (money[ptr1] >= bike[ptr2])
		{
			personal += bike[ptr2];
			ptr1++; ptr2++; cnt++;
		}
		else if (money[ptr1] + a >= bike[ptr2])
		{
			personal += money[ptr1];
			a -= (bike[ptr2] - money[ptr1]);
			ptr1++; ptr2++; cnt++;
		}
		else
		{
			ptr1++;
		}
	}
	personal -= a;
	personal = max(0,personal);
	cout << cnt << " " << personal << "\n";
}


#include <bits/stdc++.h>

using namespace std;
#define f cin
#define g cout

const int NMax = 100005;

int n,m,v;
int a[NMax],b[NMax];

bool verif(int nr){
    if(nr > n){
        return 0;
    }
    int a_ind = n;
    int ramas = v;

    for(int i = nr; i >= 1 && a_ind >= 1; --i, a_ind--){
        if(a[a_ind] < b[i]){
            ramas -= (b[i] - a[a_ind]);
        }
        if(ramas < 0){
            return 0;
        }
    }
    return 1;
}
int cautbin(int lo, int hi){
    int mid;
    while(lo <= hi){
        mid = (lo + hi) / 2;
        int ok1 = verif(mid);
        int ok2 = verif(mid + 1);
        if(ok1 == 1 && (ok2 == 0 || mid == m)){
            return mid;
        }else
        if(ok1 == 1){
            lo = mid + 1;
        }else
        if(ok2 == 0){
            hi = mid - 1;
        }
    }
    return 0;
}
int main()
{
    f >> n >> m >> v;
    for(int i = 1; i <= n; ++i){
        f >> a[i];
    }
    sort(a + 1, a + 1 + n);
    for(int j = 1; j <= m; ++j){
        f >> b[j];
    }
    sort(b + 1, b + 1 + m);
    int ans1 = cautbin(1,m);
//    g << ans1 << '\n';
    int ans2 = 0;
    int b_ind = 1;
    int s = 0;
//    for(int i = 1; i <= m; ++i){
//        g << b[i] << '\n';
//    }
    for(int i = n - ans1 + 1; i <= n; ++i){
//        g << i << '\n';
        if(b[b_ind] > a[i]){
            v -= (b[b_ind] - a[i]);
        }else{
            v += (a[i] - b[b_ind]);
        }
        b_ind++;
        s += a[i];
    }
    if(s - v < 0){
        s = v;
    }
    g << ans1 << ' ' << s - v << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define f cin
#define g cout

const ll NMax = 100005;

ll n,m,v;
ll a[NMax],b[NMax];

int main()
{
    f >> n >> m >> v;
    for(ll i = 1; i <= n; ++i){
        f >> a[i];
    }
    sort(a + 1, a + 1 + n);
    for(ll j = 1; j <= m; ++j){
        f >> b[j];
    }
    sort(b + 1, b + 1 + m);

    ll a_ind = 1, b_ind = 1;

    ll ans1 = 0, ans2 = 0;

    while(a_ind <= n && b_ind <= m){
        if(a[a_ind] >= b[b_ind]){
            ans2 += b[b_ind];
            ans1++;
            a_ind++;
            b_ind++;
        }else{
            if(b[b_ind] - a[a_ind] <= v){
                v -= (b[b_ind] - a[a_ind]);
                ans2 += (a[a_ind]);
                ans1++;
                a_ind++;
                b_ind++;
            }else{
                a_ind++;
            }
        }
    }
    if(ans2 - v < 0){
        ans2 = v;
    }
    g << ans1 << ' ' << ans2 - v << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define f cin
#define g cout

const int NMax = 100005;

int n,m,v;
int a[NMax],b[NMax];

int main()
{
    f >> n >> m >> v;
    for(int i = 1; i <= n; ++i){
        f >> a[i];
    }
    sort(a + 1, a + 1 + n);
    for(int j = 1; j <= m; ++j){
        f >> b[j];
    }
    sort(b + 1, b + 1 + m);

    int a_ind = 1, b_ind = 1;

    int ans1 = 0, ans2 = 0;

    while(a_ind <= n && b_ind <= m){
        if(a[a_ind] >= b[b_ind]){
            ans2 += b[b_ind];
            ans1++;
            a_ind++;
            b_ind++;
        }else{
            if(b[b_ind] - a[a_ind] <= v){
                v -= (b[b_ind] - a[a_ind]);
                ans2 += (a[a_ind]);
                ans1++;
                a_ind++;
                b_ind++;
            }else{
                a_ind++;
            }
        }
    }
    if(ans2 - v < 0){
        ans2 = v;
    }
    g << ans1 << ' ' << ans2 - v << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define f cin
#define g cout

const int NMax = 100005;

int n,m,v;
int a[NMax],b[NMax];

bool verif(int nr){
    int a_ind = n;
    int ramas = v;

    for(int i = nr; i >= 1 && a_ind >= 1; --i, a_ind--){
        if(a[a_ind] < b[i]){
            ramas -= (b[i] - a[a_ind]);
        }
        if(ramas < 0){
            return 0;
        }
    }
    return 1;
}
int cautbin(int lo, int hi){
    int mid;
    while(lo <= hi){
        mid = (lo + hi) / 2;
        int ok1 = verif(mid);
        int ok2 = verif(mid + 1);
        if(ok1 == 1 && (ok2 == 0 || mid == m)){
            return mid;
        }else
        if(ok1 == 1){
            lo = mid + 1;
        }else
        if(ok2 == 0){
            hi = mid - 1;
        }
    }
    return 0;
}
int main()
{
    f >> n >> m >> v;
    for(int i = 1; i <= n; ++i){
        f >> a[i];
    }
    sort(a + 1, a + 1 + n);
    for(int j = 1; j <= m; ++j){
        f >> b[j];
    }
    sort(b + 1, b + 1 + m);
    int ans1 = cautbin(1,min(n,m));
//    g << ans1 << '\n';
    int ans2 = 0;
    int b_ind = 1;
    int s = 0;
//    for(int i = 1; i <= m; ++i){
//        g << b[i] << '\n';
//    }
    for(int i = n - ans1 + 1; i <= n; ++i){
//        g << i << '\n';
        if(b[b_ind] > a[i]){
            v -= (b[b_ind] - a[i]);
        }else{
            v += (a[i] - b[b_ind]);
        }
        b_ind++;
        s += a[i];
    }
    if(s - v < 0){
        s = v;
    }
    g << ans1 << ' ' << s - v << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define f cin
#define g cout
#define ll long long

const ll NMax = 100005;

ll n,m,v;
ll a[NMax],b[NMax];

int main()
{
    f >> n >> m >> v;
    for(ll i = 1; i <= n; ++i){
        f >> a[i];
    }
    sort(a + 1, a + 1 + n);
    for(ll j = 1; j <= m; ++j){
        f >> b[j];
    }
    sort(b + 1, b + 1 + m);

    ll a_ind = 1, b_ind = 1;

    ll ans1 = 0, ans2 = 0;

    while(a_ind <= n && b_ind <= m){
        if(a[a_ind] >= b[b_ind]){
            ans2 += b[b_ind];
            ans1++;
            a_ind++;
            b_ind++;
        }else{
            if(b[b_ind] - a[a_ind] <= v){
                v -= (b[b_ind] - a[a_ind]);
                ans2 += (a[a_ind]);
                ans1++;
                a_ind++;
                b_ind++;
            }else{
                a_ind++;
            }
        }
    }
    if(ans2 - v < 0){
        ans2 = v;
    }
    g << ans1 << ' ' << ans2 - v << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define f cin
#define g cout

const int NMax = 100005;

int n,m,v;
int a[NMax],b[NMax];

bool verif(int nr){
    int a_ind = n;
    int ramas = v;

    for(int i = nr; i >= 1 && a_ind >= 1; --i, a_ind--){
        if(a[a_ind] < b[i]){
            ramas -= (b[i] - a[a_ind]);
        }
        if(ramas < 0){
            return 0;
        }
    }
    return 1;
}
int cautbin(int lo, int hi){
    int mid;
    while(lo <= hi){
        mid = (lo + hi) / 2;
        int ok1 = verif(mid);
        int ok2 = verif(mid + 1);
        if(ok1 == 1 && (ok2 == 0 || mid == m)){
            return mid;
        }else
        if(ok1 == 1){
            lo = mid + 1;
        }else
        if(ok2 == 0){
            hi = mid - 1;
        }
    }
    return 0;
}
int main()
{
    f >> n >> m >> v;
    for(int i = 1; i <= n; ++i){
        f >> a[i];
    }
    sort(a + 1, a + 1 + n);
    for(int j = 1; j <= m; ++j){
        f >> b[j];
    }
    sort(b + 1, b + 1 + m);
    int ans1 = cautbin(1,m);
//    g << ans1 << '\n';
    int ans2 = 0;
    int b_ind = 1;
    int s = 0;
//    for(int i = 1; i <= m; ++i){
//        g << b[i] << '\n';
//    }
    for(int i = n - ans1 + 1; i <= n; ++i){
//        g << i << '\n';
        if(b[b_ind] > a[i]){
            v -= (b[b_ind] - a[i]);
        }else{
            v += (a[i] - b[b_ind]);
        }
        b_ind++;
        s += a[i];
    }
    if(s - v < 0){
        s = v;
    }
    g << ans1 << ' ' << s - v << '\n';
    return 0;
}


//Problem #A from Codeforces zepto
#include <cmath>

#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

#define fi first
#define se second
#define int64 long long

const int kMaxN = 2005;

int t[kMaxN], h[kMaxN], m[kMaxN];

int n, x, mx;

vector<pair<int, int> > pq[2];
priority_queue<int> hp[2];

void solve() {
	sort(pq[0].begin(), pq[0].end());
	sort(pq[1].begin(), pq[1].end());

	int now = 0;
	int H = x;

	while (hp[0].size() != 0)
		hp[0].pop();
	while (hp[1].size() != 0)
		hp[1].pop();

 	int st[2];
	st[0] = st[1] = 0;

	while (true) {
	for (int t = 0; t < 2; ++t) {
 		for (int i = 0; i < 2; ++i) {
			while (st[i] < int(pq[i].size()) and pq[i][st[i]].first <= H) {
				hp[i].push(-pq[i][st[i]].second);
				++st[i];
			}
		}

		if (hp[t].size() == 0) {
			if (mx < now)
				mx = now;
			return ;
		}

		int act = hp[t].top();
		hp[t].pop();
		H -= act;
		++now;
	}
	}
}

int main() {
	cin >> n >> x;
	for (int i = 1; i <= n; ++i) {
		cin >> t[i] >> h[i] >> m[i];
	}

	for (int i = 1; i <= n; ++i) {
		pq[t[i]].push_back(make_pair(h[i], m[i]));
	}

	solve();

	pq[0].clear();
	pq[1].clear();

	for (int i = 1; i <= n; ++i) {
		pq[t[i] ^ 1].push_back(make_pair(h[i], m[i]));
	}

	solve();

	cout << mx << '\n';

    return 0;
}


//Problem #A from Codeforces zepto
#include <cmath>

#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

#define fi first
#define se second
#define int64 long long

const int kMaxN = 2005;

int t[kMaxN], h[kMaxN], m[kMaxN];

int n, x, mx;

vector<pair<int, int> > pq[2];
priority_queue<int> hp[2];

void solve() {
	sort(pq[0].begin(), pq[0].end());
	sort(pq[1].begin(), pq[1].end());

	int now = 0;
	int H = x;

	while (hp[0].size() != 0)
		hp[0].pop();
	while (hp[1].size() != 0)
		hp[1].pop();

 	int st[2];
	st[0] = st[1] = 0;

	while (true) {
	for (int t = 0; t < 2; ++t) {
 		for (int i = 0; i < 2; ++i) {
			while (st[i] < int(pq[i].size()) and pq[i][st[i]].first < H) {
				hp[i].push(-pq[i][st[i]].second);
				++st[i];
			}
		}

		if (hp[t].size() == 0) {
			if (mx < now)
				mx = now;
			return ;
		}

		int act = hp[t].top();
		hp[t].pop();
		H -= act;
		++now;
	}
	}
}

int main() {
	cin >> n >> x;
	for (int i = 1; i <= n; ++i) {
		cin >> t[i] >> h[i] >> m[i];
	}

	for (int i = 1; i <= n; ++i) {
		pq[t[i]].push_back(make_pair(h[i], m[i]));
	}

	solve();

	pq[0].clear();
	pq[1].clear();

	for (int i = 1; i <= n; ++i) {
		pq[t[i] ^ 1].push_back(make_pair(h[i], m[i]));
	}

	solve();

	cout << mx << '\n';

    return 0;
}


//Problem #A from Codeforces zepto
#include <cmath>

#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

#define fi first
#define se second
#define int64 long long

const int kMaxN = 2005;

int t[kMaxN], h[kMaxN], m[kMaxN];

int n, x, mx;

vector<pair<int, int> > pq[2];
priority_queue<int> hp[2];

void solve() {
	sort(pq[0].begin(), pq[0].end());
	sort(pq[1].begin(), pq[1].end());

	int now = 0;
	int H = x;

	while (hp[0].size() != 0)
		hp[0].pop();
	while (hp[1].size() != 0)
		hp[1].pop();

 	int st[2];
	st[0] = st[1] = 0;

	while (true) {
	for (int t = 0; t < 2; ++t) {
 		for (int i = 0; i < 2; ++i) {
			while (st[i] < int(pq[i].size()) and pq[i][st[i]].first <= H) {
				hp[i].push(pq[i][st[i]].second);
				++st[i];
			}
		}

		if (hp[t].size() == 0) {
			if (mx < now)
				mx = now;
			return ;
		}
		int act = hp[t].top();
		hp[t].pop();
		H += act;
		++now;
	}
	}
}

int main() {
	cin >> n >> x;
	for (int i = 1; i <= n; ++i) {
		cin >> t[i] >> h[i] >> m[i];
	}

	for (int i = 1; i <= n; ++i) {
		pq[t[i]].push_back(make_pair(h[i], m[i]));
	}

	solve();

	pq[0].clear();
	pq[1].clear();

	for (int i = 1; i <= n; ++i) {
		pq[t[i] ^ 1].push_back(make_pair(h[i], m[i]));
	}

	solve();

	cout << mx << '\n';

    return 0;
}


//Problem #A from Codeforces zepto
#include <cmath>

#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

#define fi first
#define se second
#define int64 long long

const int kMaxN = 2005;

int t[kMaxN], h[kMaxN], m[kMaxN];

int n, x, mx;

vector<pair<int, int> > pq[2];
priority_queue<int> hp[2];

void solve() {
	sort(pq[0].begin(), pq[0].end());
	sort(pq[1].begin(), pq[1].end());

	int now = 0;
	int H = x;

	while (not hp[0].empty())
		hp[0].pop();
	while (not hp[1].empty())
		hp[1].pop();

 	int st[2];
	st[0] = st[1] = 0;

	while (true) {
 		for (int i = 0; i < 2; ++i) {
			while (st[i] < int(pq[i].size()) and pq[i][st[i]].first <= H) {
				hp[i].push(-pq[i][st[i]].second);
				++st[i];
			}
		}

		if (hp[0].empty())
			break;

		int act = hp[0].top();
		hp[0].pop();
		H -= act;
		++now;

  		for (int i = 0; i < 2; ++i) {
			while (st[i] < int(pq[i].size()) and pq[i][st[i]].first <= H) {
				hp[i].push(-pq[i][st[i]].second);
				++st[i];
			}
		}

		if (hp[1].empty())
			break;
		act = hp[1].top();
		hp[1].pop();
		H -= act;
		++now;
	}
	if (mx < now)
		mx = now;
}

int main() {
	cin >> n >> x;
	for (int i = 1; i <= n; ++i) {
		cin >> t[i] >> h[i] >> m[i];
	}

	for (int i = 1; i <= n; ++i) {
		pq[t[i]].push_back(make_pair(h[i], m[i]));
	}

	solve();

	pq[0].clear();
	pq[1].clear();

	for (int i = 1; i <= n; ++i) {
		pq[t[i] ^ 1].push_back(make_pair(h[i], m[i]));
	}

	solve();

	cout << mx << '\n';

    return 0;
}


//Problem #A from Codeforces zepto
#include <cmath>

#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

#define fi first
#define se second
#define int64 long long

const int kMaxN = 2005;

int t[kMaxN], h[kMaxN], m[kMaxN];

int n, x, mx;

vector<pair<int, int> > pq[2];
priority_queue<int> hp[2];

void solve() {
	sort(pq[0].begin(), pq[0].end());
	sort(pq[1].begin(), pq[1].end());

	int now = 0;
	int H = x;

	while (hp[0].size() != 0)
		hp[0].pop();
	while (hp[1].size() != 0)
		hp[1].pop();

 	int st[2];
	st[0] = st[1] = 0;

	while (true) {
	for (int t = 0; t < 2; ++t) {
 		for (int i = 0; i < 2; ++i) {
			while (st[i] < int(pq[i].size()) and pq[i][st[i]].first <= H) {
				hp[i].push(-pq[i][st[i]].second);
				++st[i];
			}
		}

		if (hp[t].size() == 0)
			break;

		int act = hp[t].top();
		hp[t].pop();
		H -= act;
		++now;
	}

	}
	if (mx < now)
		mx = now;
}

int main() {
	cin >> n >> x;
	for (int i = 1; i <= n; ++i) {
		cin >> t[i] >> h[i] >> m[i];
	}

	for (int i = 1; i <= n; ++i) {
		pq[t[i]].push_back(make_pair(h[i], m[i]));
	}

	solve();

	pq[0].clear();
	pq[1].clear();

	for (int i = 1; i <= n; ++i) {
		pq[t[i] ^ 1].push_back(make_pair(h[i], m[i]));
	}

	solve();

	cout << mx << '\n';

    return 0;
}


#include <iostream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>

using namespace std;

int main()
{
    int n, x;
    cin >> n >> x;
    vector<pair<int, int> > a, b;
    for (int i = 0; i < n; ++i)
    {
        int q, w, e;
        cin >> q >> w >> e;
        if (q == 0)
            a.push_back(make_pair(w, e));
        else
            b.push_back(make_pair(w, e));
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    if (a[0].first > b[0].first)
        swap(a, b);
    int ans = 0;
    int i = 0;
    while (i < a.size() && i < b.size())
    {
        if (a[i].first <= x)
        {
            ans++;
            x += a[i].second;
        }
        else
            break;
        if (b[i].first <= x)
        {
            ans++;
            x += b[i].second;
        }
        else
            break;
        i++;
    }
    cout << ans << endl;

}


#include <iostream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>

using namespace std;

int px;

int solve(vector<pair<int, int> > a, vector<pair<int, int> > b)
{
    int x = px;
    int res = 0;
    vector<bool> useda(a.size(), false), usedb(b.size(), false);
    while (1)
    {
        int c = -1;
        for (int i = 0; i < a.size(); ++i)
            if (!useda[i] && a[i].first <= x && (c == -1 || (a[c].second < a[i].second)))
                c = i;
        if (c != -1)
        {
            x += a[c].second;
            useda[c] = true;
            res++;
        }
        else
            break;
        c = -1;
        for (int i = 0; i < b.size(); ++i)
            if (!usedb[i] && b[i].first <= x && (c == -1 || (b[c].second < b[i].second)))
                c = i;
        if (c != -1)
        {
            x += b[c].second;
            usedb[c] = true;
            res++;
        }
        else
            break;
    }
    return res;
}

int main()
{
    int n;
    cin >> n >> px;
    vector<pair<int, int> > a, b;
    for (int i = 0; i < n; ++i)
    {
        int q, w, e;
        cin >> q >> w >> e;
        if (q == 0)
            a.push_back(make_pair(w, e));
        else
            b.push_back(make_pair(w, e));
    }
    cout << max(solve(a, b), solve(b, a)) << endl;
    return 0;
}


#include <iostream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>

using namespace std;

int main()
{
    int n, px;
    cin >> n >> px;
    vector<pair<int, int> > a, b;
    for (int i = 0; i < n; ++i)
    {
        int q, w, e;
        cin >> q >> w >> e;
        if (q == 0)
            a.push_back(make_pair(w, -e));
        else
            b.push_back(make_pair(w, -e));
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    int ans1 = 0;
    for (int i = 0, x = px; i < a.size(); ++i)
    {
        if (a[i].first <= x)
        {
            ans1++;
            x -= a[i].second;
        }
        else
            break;
        if (i < b.size() && b[i].first <= x)
        {
            ans1++;
            x -= b[i].second;
        }
        else
            break;
    }
    int ans2 = 0;
    swap(a, b);
    for (int i = 0, x = px; i < a.size(); ++i)
    {
        if (a[i].first <= x)
        {
            ans2++;
            x -= a[i].second;
        }
        else
            break;
        if (i < b.size() && b[i].first <= x)
        {
            ans2++;
            x -= b[i].second;
        }
        else
            break;
    }
    cout << max(ans1, ans2) << endl;
    return 0;
}


#include <iostream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>

using namespace std;

int main()
{
    int n, x;
    cin >> n >> x;
    vector<pair<int, int> > a, b;
    for (int i = 0; i < n; ++i)
    {
        int q, w, e;
        cin >> q >> w >> e;
        if (q == 0)
            a.push_back(make_pair(w, e));
        else
            b.push_back(make_pair(w, e));
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    int ans1 = 0;
    int i = 0;
    int px = x;
    while (i < a.size())
    {
        if (a[i].first <= x)
        {
            ans1++;
            x += a[i].second;
        }
        else
            break;
        if (i < b.size() && b[i].first <= x)
        {
            ans1++;
            x += b[i].second;
        }
        else
            break;
        i++;
    }
    i = 0;
    x = px;
    int ans2 = 0;
    swap(a, b);
    while (i < a.size())
    {
        if (a[i].first <= x)
        {
            ans2++;
            x += a[i].second;
        }
        else
            break;
        if (i < b.size() && b[i].first <= x)
        {
            ans2++;
            x += b[i].second;
        }
        else
            break;
        i++;
    }
    cout << max(ans1, ans2) << endl;

}


#pragma comment(linker, "/STACK:66777216")
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

const int N = 2007;
bool dp[N][N][2];
int sa[N], sb[N];

int main()
{
#ifdef Armenoid
   freopen("input.txt", "r", stdin);
   //freopen("output.txt", "w", stdout);
#endif

   int n, x;
   cin >> n >> x;

   vector<pair<int, int> > A, B;

   for (int i = 0; i < n; ++i)
   {
      int ty;
      cin >> ty;
      int h, m;
      cin >> h >> m;
      if (ty == 0) A.pb(mp(h, m));
      else B.pb(mp(h, m));
   }
   sort(all(A));
   sort(all(B));
   int ans = 0;
   int na = sz(A);
   int nb = sz(B);
   dp[0][0][0] = true;
   dp[0][0][1] = true;
   for (int i = 1; i <= na; ++i) sa[i] = sa[i - 1] + A[i - 1].second;
   for (int i = 1; i <= nb; ++i) sb[i] = sb[i - 1] + B[i - 1].second;
   for (int i = 0; i <= na; ++i)
   {
      for (int j = 0; j <= nb; ++j)
      {
         for (int c = 0; c < 2; ++c)
         {
            if (!dp[i][j][c]) continue;
            ans = max(ans, i + j);
            if (c == 0 && j != nb && B[j].second <= x + sa[i] + sb[j]) dp[i][j + 1][1 - c] = true;
            if (c == 1 && i != na && A[i].second <= x + sa[i] + sb[j]) dp[i + 1][j][1 - c] = true;
         }
      }
   }

   cout << ans << endl;

   return 0;

}


#pragma comment(linker, "/STACK:66777216")
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

int solve(int x, vector<pair<int, int> > A, vector<pair<int, int> > B)
{
   int i = 0, j = 0;
   int na = sz(A);
   int nb = sz(B);
   vector<bool> a(na, false);
   vector<bool> b(nb, false);
   while (true)
   {
      if ((i + j) & 1)
      {
         // B
         if (j == nb) break;
         int pos = -1, maxi = -1;
         for (int u = 0; u < nb; ++u)
         {
            if (!b[u])
            {
               if (B[u].second > maxi && B[u].first <= x)
               {
                  maxi = B[u].second;
                  pos = u;
               }
            }
         }
         if (pos == -1) break;
         b[pos] = true;
         x += maxi;
         ++j;
      }
      else
      {
         if (i == na) break;
         int pos = -1, maxi = -1;
         for (int u = 0; u < na; ++u)
         {
            if (!a[u])
            {
               if (A[u].second > maxi && A[u].first <= x)
               {
                  maxi = A[u].second;
                  pos = u;
               }
            }
         }
         if (pos == -1) break;
         a[pos] = true;
         x += maxi;
         ++i;
      }
   }
   return i + j;
}

int main()
{
#ifdef Armenoid
   freopen("input.txt", "r", stdin);
   //freopen("output.txt", "w", stdout);
#endif

   int n, x;
   cin >> n >> x;

   vector<pair<int, int> > A, B;

   for (int i = 0; i < n; ++i)
   {
      int ty;
      cin >> ty;
      int h, m;
      cin >> h >> m;
      if (ty == 0) A.pb(mp(h, m));
      else B.pb(mp(h, m));
   }
   sort(all(A));
   sort(all(B));
   int ans = max(solve(x, A, B), solve(x, B, A));
   cout << ans << endl;
   return 0;
}


#pragma comment(linker, "/STACK:66777216")
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

const int N = 2007;
bool dp[N][N][2];
int sa[N], sb[N];

int main()
{
#ifdef Armenoid
   freopen("input.txt", "r", stdin);
   //freopen("output.txt", "w", stdout);
#endif

   int n, x;
   cin >> n >> x;

   vector<pair<int, int> > A, B;

   for (int i = 0; i < n; ++i)
   {
      int ty;
      cin >> ty;
      int h, m;
      cin >> h >> m;
      if (ty == 0) A.pb(mp(h, m));
      else B.pb(mp(h, m));
   }
   sort(all(A));
   sort(all(B));
   int ans = 0;
   int na = sz(A);
   int nb = sz(B);
   dp[0][0][0] = true;
   dp[0][0][1] = true;
   for (int i = 1; i <= na; ++i) sa[i] = sa[i - 1] + A[i - 1].second;
   for (int i = 1; i <= nb; ++i) sb[i] = sb[i - 1] + B[i - 1].second;
   for (int i = 0; i <= na; ++i)
   {
      for (int j = 0; j <= nb; ++j)
      {
         for (int c = 0; c < 2; ++c)
         {
            if (!dp[i][j][c]) continue;
            ans = max(ans, i + j);
            if (c == 0 && j != nb && B[j].first <= x + sa[i] + sb[j]) dp[i][j + 1][1 - c] = true;
            if (c == 1 && i != na && A[i].first <= x + sa[i] + sb[j]) dp[i + 1][j][1 - c] = true;
         }
      }
   }

   cout << ans << endl;

   return 0;
}


#include <bits/stdc++.h>
#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it,X) for(auto it=(X).begin();it!=(X).end();it++)
#define numa(x,a) for(auto x: a)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define pb push_back
#define pf push_front
#define sec second
#define sz(x) ((int)(x).size())
#define ALL( c ) (c).begin(), (c).end()
#define gcd(a,b) __gcd(a,b)
#define mem(x,n) memset(x,n,sizeof(x))
#define endl "\n"
using namespace std;
template <int POS, class TUPLE> void deploy(std::ostream &os, const TUPLE &tuple){}
template <int POS, class TUPLE, class H, class ...Ts> void deploy(std::ostream &os, const TUPLE &t){ os << (POS == 0 ? "" : ", ") << get<POS>(t); deploy<POS + 1, TUPLE, Ts...>(os, t); }
template <class T> std::ostream& operator<<(std::ostream &os, std::vector<T> &v){ int remain = v.size(); os << "{"; for(auto e: v) os << e << (--remain == 0 ? "}" : ", "); return os; }
template <class T> std::ostream& operator<<(std::ostream &os, std::set<T> &v){ int remain = v.size(); os << "{"; for(auto e: v) os << e << (--remain == 0 ? "}" : ", "); return os; }
template <class T, class K> std::ostream& operator<<(std::ostream &os, std::map<T, K> &mp){ int remain = mp.size(); os << "{"; for(auto e: mp) os << "(" << e.first << " -> " << e.second << ")" << (--remain == 0 ? "}" : ", "); return os; }
#define DEBUG1(var0) { std::cerr << (#var0) << "=" << (var0) << endl; }
#define DEBUG2(var0, var1) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG1(var1); }
#define DEBUG3(var0, var1, var2) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG2(var1,var2); }
#define DEBUG4(var0, var1, var2, var3) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG3(var1,var2,var3); }
typedef long long ll;

int t[2010];
int h[2010];
int m[2010];
bool used[2010];

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int n,x;
  cin >> n >> x;

  rep(i,n){
    cin >> t[i] >> h[i] >> m[i];
  }

  int ans = 0;
  rep(type,2){
    mem(used,0);
    int now = x;
    int now_type = type;
    int cnt = 0;
    while(1){
      int k = -1;
      int now_big = 0;
      rep(j,n){
        if(!used[j]){
          if(now >= h[j] && t[j] == now_type && m[j] > now_big){
            k = j;
            now_big = m[j];
          }
        }
      }
      if(k == -1){
        break;
      }
      used[k] = true;
      x += m[k];
      cnt += 1;
      ans = max(ans,cnt);
      now_type = 1-now_type;
    }
  }
  cout << ans << endl;
  return 0;
}


#include <bits/stdc++.h>
#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it,X) for(auto it=(X).begin();it!=(X).end();it++)
#define numa(x,a) for(auto x: a)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define pb push_back
#define pf push_front
#define sec second
#define sz(x) ((int)(x).size())
#define ALL( c ) (c).begin(), (c).end()
#define gcd(a,b) __gcd(a,b)
#define mem(x,n) memset(x,n,sizeof(x))
#define endl "\n"
using namespace std;
template <int POS, class TUPLE> void deploy(std::ostream &os, const TUPLE &tuple){}
template <int POS, class TUPLE, class H, class ...Ts> void deploy(std::ostream &os, const TUPLE &t){ os << (POS == 0 ? "" : ", ") << get<POS>(t); deploy<POS + 1, TUPLE, Ts...>(os, t); }
template <class T> std::ostream& operator<<(std::ostream &os, std::vector<T> &v){ int remain = v.size(); os << "{"; for(auto e: v) os << e << (--remain == 0 ? "}" : ", "); return os; }
template <class T> std::ostream& operator<<(std::ostream &os, std::set<T> &v){ int remain = v.size(); os << "{"; for(auto e: v) os << e << (--remain == 0 ? "}" : ", "); return os; }
template <class T, class K> std::ostream& operator<<(std::ostream &os, std::map<T, K> &mp){ int remain = mp.size(); os << "{"; for(auto e: mp) os << "(" << e.first << " -> " << e.second << ")" << (--remain == 0 ? "}" : ", "); return os; }
#define DEBUG1(var0) { std::cerr << (#var0) << "=" << (var0) << endl; }
#define DEBUG2(var0, var1) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG1(var1); }
#define DEBUG3(var0, var1, var2) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG2(var1,var2); }
#define DEBUG4(var0, var1, var2, var3) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG3(var1,var2,var3); }
typedef long long ll;

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int n,x;
  cin >> n >> x;

  vector <pair <int,int> > nums1;
  vector <pair <int,int> > nums2;

  rep(i,n){
    int t,h,m;
    cin >> t >> h >> m;
    if(t == 0){
      nums1.pb(mp(h,m));
    }else{
      nums2.pb(mp(h,m));
    }
  }

  sort(ALL(nums1));
  sort(ALL(nums2));

  //nums1 -> nums2
  int ans = 0;
  int now = x;
  for(int i = 0,j = 0; i < sz(nums1) && i < sz(nums2);j++){
    if(i % 2 == 0){
      if(now >= nums1[i].fir){
        ans += 1;
        now += nums1[i].sec;
      }else{
        break;
      }
    }else{
      if(now >= nums2[i].fir){
        ans += 1;
        now += nums2[i].sec;
      }else{
        break;
      }
    }
    if(j % 2 == 1){
      i += 1;
    }
  }
  now = x;
  int ans2 = 0;
  //nums2 -> nums1
  for(int i = 0,j = 0; i < sz(nums1) && i < sz(nums2);j++){
    if(i % 2 == 0){
      if(now >= nums2[i].fir){
        ans2 += 1;
        now += nums2[i].sec;
      }else{
        break;
      }
    }else{
      if(now >= nums1[i].fir){
        ans2 += 1;
        now += nums1[i].sec;
      }else{
        break;
      }
    }
    if(j % 2 == 1){
      i += 1;
    }
  }
  cout << max(ans,ans2) << endl;
  return 0;
}


#include <bits/stdc++.h>
#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it,X) for(auto it=(X).begin();it!=(X).end();it++)
#define numa(x,a) for(auto x: a)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define pb push_back
#define pf push_front
#define sec second
#define sz(x) ((int)(x).size())
#define ALL( c ) (c).begin(), (c).end()
#define gcd(a,b) __gcd(a,b)
#define mem(x,n) memset(x,n,sizeof(x))
#define endl "\n"
using namespace std;
template <int POS, class TUPLE> void deploy(std::ostream &os, const TUPLE &tuple){}
template <int POS, class TUPLE, class H, class ...Ts> void deploy(std::ostream &os, const TUPLE &t){ os << (POS == 0 ? "" : ", ") << get<POS>(t); deploy<POS + 1, TUPLE, Ts...>(os, t); }
template <class T> std::ostream& operator<<(std::ostream &os, std::vector<T> &v){ int remain = v.size(); os << "{"; for(auto e: v) os << e << (--remain == 0 ? "}" : ", "); return os; }
template <class T> std::ostream& operator<<(std::ostream &os, std::set<T> &v){ int remain = v.size(); os << "{"; for(auto e: v) os << e << (--remain == 0 ? "}" : ", "); return os; }
template <class T, class K> std::ostream& operator<<(std::ostream &os, std::map<T, K> &mp){ int remain = mp.size(); os << "{"; for(auto e: mp) os << "(" << e.first << " -> " << e.second << ")" << (--remain == 0 ? "}" : ", "); return os; }
#define DEBUG1(var0) { std::cerr << (#var0) << "=" << (var0) << endl; }
#define DEBUG2(var0, var1) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG1(var1); }
#define DEBUG3(var0, var1, var2) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG2(var1,var2); }
#define DEBUG4(var0, var1, var2, var3) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG3(var1,var2,var3); }
typedef long long ll;

int t[2010];
int h[2010];
int m[2010];
bool used[2010];

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int n,x;
  cin >> n >> x;

  rep(i,n){
    cin >> t[i] >> h[i] >> m[i];
  }

  int ans = 0;
  rep(type,2){
    mem(used,0);
    int now = x;
    int now_type = type;
    int cnt = 0;
    while(1){
      int k = -1;
      int now_big = 0;
      rep(j,n){
        if(!used[j]){
          if(now >= h[j] && t[j] == now_type && m[j] > now_big){
            k = j;
            now_big = m[j];
          }
        }
      }
      if(k == -1){
        break;
      }
      used[k] = true;
      now += m[k];
      cnt += 1;
      ans = max(ans,cnt);
      now_type = 1-now_type;
    }
  }
  cout << ans << endl;
  return 0;
}


#include <bits/stdc++.h>
#define fr(i,a,b) for(int i = (a); i < (b); ++i)
#define rep(i,n) fr(i,0,n)
#define cl(a,b) memset((a), (b), sizeof(a))
#define all(c) (c).begin(), (c).end()
#define _ << ", " <<
#define db(x) cerr << #x " == " << x << endl

using namespace std;

typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long long ll;
const int inf = 0x3f3f3f3f;

int solve(int tipo, set<tuple<int,int,int> > s, int n, int x) {
  int rsp = 0;
  int prev_type = tipo;
  rep(__,n) {
    tuple<int,int,int> best = make_tuple(-1,0,0);
    for (set<tuple<int,int,int> >::iterator it = s.begin(); it != s.end(); ++it) {
      int t, h, m;
      tie(t,h,m) = *it;
      if (t != prev_type && h <= x) {
        if (m > get<2>(best)) best = *it;
      }
    }
    if (get<0>(best) != -1) {
      x += get<2>(best);
      s.erase(best);
      ++rsp;
      prev_type = get<0>(best);
    }
  }
  return rsp;
}

int main() {
  //ios_base::sync_with_stdio(false);

  int n, x;
  scanf("%d %d", &n, &x);

  set<tuple<int,int,int> > s;

  rep(i,n) {
    int t, h, m;
    scanf("%d %d %d", &t, &h, &m);
    s.insert(make_tuple(t,h,m));
  }

  printf("%d\n", max(solve(0, s, n, x), solve(1, s, n, x)));

  return 0;
}


#include <bits/stdc++.h>
#define fr(i,a,b) for(int i = (a); i < (b); ++i)
#define rep(i,n) fr(i,0,n)
#define cl(a,b) memset((a), (b), sizeof(a))
#define all(c) (c).begin(), (c).end()
#define _ << ", " <<
#define db(x) cerr << #x " == " << x << endl

using namespace std;

typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long long ll;
const int inf = 0x3f3f3f3f;

int main() {
  //ios_base::sync_with_stdio(false);

  int n, x;
  scanf("%d %d", &n, &x);

  set<tuple<int,int,int> > s;

  rep(i,n) {
    int t, h, m;
    scanf("%d %d %d", &t, &h, &m);
    s.insert(make_tuple(t,h,m));
  }

  int rsp = 0;
  int prev_type = -1;
  rep(__,n) {
    tuple<int,int,int> best = make_tuple(-1,0,0);
    for (set<tuple<int,int,int> >::iterator it = s.begin(); it != s.end(); ++it) {
      int t, h, m;
      tie(t,h,m) = *it;
      if (t != prev_type && h <= x) {
        if (m > get<2>(best)) best = *it;
      }
    }
    if (get<0>(best) != -1) {
      x += get<2>(best);
      s.erase(best);
      ++rsp;
      prev_type = get<0>(best);
    }
  }

  printf("%d\n", rsp);

  return 0;
}


#include <bits/stdc++.h>
#define fr(i,a,b) for(int i = (a); i < (b); ++i)
#define rep(i,n) fr(i,0,n)
#define cl(a,b) memset((a), (b), sizeof(a))
#define all(c) (c).begin(), (c).end()
#define _ << ", " <<
#define db(x) cerr << #x " == " << x << endl

using namespace std;

typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long long ll;
const int inf = 0x3f3f3f3f;

int solve(int tipo, vector<tuple<int,int,int> > s, int n, int x) {
  int rsp = 0;
  int prev_type = tipo;
  rep(__,n) {
    tuple<int,int,int> best = make_tuple(-1,0,0);
    for (vector<tuple<int,int,int> >::iterator it = s.begin(); it != s.end(); ++it) {
      int t, h, m;
      tie(t,h,m) = *it;
      if (t != prev_type && h <= x) {
        if (m > get<2>(best)) best = *it;
      }
    }
    if (get<0>(best) != -1) {
      x += get<2>(best);
      s.erase(find(all(s), best));
      ++rsp;
      prev_type = get<0>(best);
    }
  }
  return rsp;
}

int main() {
  //ios_base::sync_with_stdio(false);

  int n, x;
  scanf("%d %d", &n, &x);

  vector<tuple<int,int,int> > s;

  rep(i,n) {
    int t, h, m;
    scanf("%d %d %d", &t, &h, &m);
    s.push_back(make_tuple(t,h,m));
  }

  printf("%d\n", max(solve(0, s, n, x), solve(1, s, n, x)));

  return 0;
}


#include <bits/stdc++.h>
#define fr(i,a,b) for(int i = (a); i < (b); ++i)
#define rep(i,n) fr(i,0,n)
#define cl(a,b) memset((a), (b), sizeof(a))
#define all(c) (c).begin(), (c).end()
#define _ << ", " <<
#define db(x) cerr << #x " == " << x << endl

using namespace std;

typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long long ll;
const int inf = 0x3f3f3f3f;

int solve(int tipo, set<tuple<int,int,int> >& s, int n, int x) {
  int rsp = 0;
  int prev_type = tipo;
  rep(__,n) {
    tuple<int,int,int> best = make_tuple(-1,0,0);
    for (set<tuple<int,int,int> >::iterator it = s.begin(); it != s.end(); ++it) {
      int t, h, m;
      tie(t,h,m) = *it;
      if (t != prev_type && h <= x) {
        if (m > get<2>(best)) best = *it;
      }
    }
    if (get<0>(best) != -1) {
      x += get<2>(best);
      s.erase(best);
      ++rsp;
      prev_type = get<0>(best);
    }
  }
  return rsp;
}

int main() {
  //ios_base::sync_with_stdio(false);

  int n, x;
  scanf("%d %d", &n, &x);

  set<tuple<int,int,int> > s;

  rep(i,n) {
    int t, h, m;
    scanf("%d %d %d", &t, &h, &m);
    s.insert(make_tuple(t,h,m));
  }

  printf("%d\n", max(solve(0, s, n, x), solve(1, s, n, x)));

  return 0;
}


#include <bits/stdc++.h>
#define fr(i,a,b) for(int i = (a); i < (b); ++i)
#define rep(i,n) fr(i,0,n)
#define cl(a,b) memset((a), (b), sizeof(a))
#define all(c) (c).begin(), (c).end()
#define _ << ", " <<
#define db(x) cerr << #x " == " << x << endl

using namespace std;

typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long long ll;
const int inf = 0x3f3f3f3f;

int solve(int tipo, multiset<tuple<int,int,int> > s, int n, int x) {
  int rsp = 0;
  int prev_type = tipo;
  rep(__,n) {
    tuple<int,int,int> best = make_tuple(-1,0,0);
    for (multiset<tuple<int,int,int> >::iterator it = s.begin(); it != s.end(); ++it) {
      int t, h, m;
      tie(t,h,m) = *it;
      if (t != prev_type && h <= x) {
        if (m > get<2>(best)) best = *it;
      }
    }
    if (get<0>(best) != -1) {
      x += get<2>(best);
      s.erase(best);
      ++rsp;
      prev_type = get<0>(best);
    }
  }
  return rsp;
}

int main() {
  //ios_base::sync_with_stdio(false);

  int n, x;
  scanf("%d %d", &n, &x);

  multiset<tuple<int,int,int> > s;

  rep(i,n) {
    int t, h, m;
    scanf("%d %d %d", &t, &h, &m);
    s.insert(make_tuple(t,h,m));
  }

  printf("%d\n", max(solve(0, s, n, x), solve(1, s, n, x)));

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

main () {
    int n, x;
    cin >> n >> x;
    multiset <pair <int, int> > a[2], b[2];
    for (int i = 0; i < n; ++i) {
        int t, h, m;
        cin >> t >> h >> m;
        a[t].insert ({h, m});
        b[t].insert ({h, m});
    }
    int x1 = x, x2 = x;
    int res = 0, ans = 0;
    for (;!a[0].empty () && !a[1].empty ();) {
        auto it = a[0].end ();
        int m = -1;
        for (auto y : a[0]) {
            if (y.first <= x1 && m < y.second) {
                it = a[0].lower_bound(y);
                m = y.second;
            }
        }
        if (it == a[0].end ()) break;
        ++res;
        x1 += m;
        a[0].erase (it);
        it = a[1].end ();
        m = -1;
        for (auto y : a[1]) {
            if (y.first <= x1 && m < y.second) {
                it = a[1].lower_bound(y);
                m = y.second;
            }
        }
        if (it == a[1].end ()) break;
        ++res;
        x1 += m;
        a[1].erase (it);
    }
    for (;!b[0].empty () && !b[1].empty ();) {
        auto it = b[1].end ();
        int m = -1;
        for (auto y : b[1]) {
            if (y.first <= x2 && m < y.second) {
                it = b[1].lower_bound(y);
                m = y.second;
            }
        }
        if (it == b[1].end ()) break;
        ++ans;
        x2 += m;
        b[1].erase (it);
        it = b[0].end ();
        m = -1;
        for (auto y : b[0]) {
            if (y.first <= x2 && m < y.second) {
                it = b[0].lower_bound(y);
                m = y.second;
            }
        }
        if (it == b[0].end ()) break;
        x2 += m;
        ++ans;
        b[0].erase (it);
    }
    cout << max(ans, res);
}


#include <bits/stdc++.h>

using namespace std;

main () {
    int n, x;
    cin >> n >> x;
    vector <pair <int, int> > a[2];
    vector <bool> used[2];
    for (int i = 0; i < n; ++i) {
        int t, h, m;
        cin >> t >> h >> m;
        a[t].push_back ({h, m});
        used[t].push_back (0);
    }
    int x1 = x, x2 = x;
    int res = 0, ans = 0;
    for (;;) {
        int m = -1;
        int k = -1;
        for (int j = 0; j < a[0].size (); ++j) {
            if (!used[0][j] && a[0][j].first <= x1 && a[0][j].second > m) {
                k = j;
                m = a[0][j].second;
            }
        }
        if (k == -1) break;
        used[0][k] = true;
        x1 += m;
        ++res;
        m = -1;
        k = -1;
        for (int j = 0; j < a[1].size (); ++j) {
            if (!used[1][j] && a[1][j].first <= x1 && a[1][j].second > m) {
                k = j;
                m = a[1][j].second;
            }
        }
        if (k == -1) break;
        used[1][k] = true;
        x1 += m;
        ++res;
    }
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < a[i].size (); ++j) used[i][j] = false;
    }
    for (;;) {
        int m = -1;
        int k = -1;
        for (int j = 0; j < a[1].size (); ++j) {
            if (!used[1][j] && a[1][j].first <= x2 && a[1][j].second > m) {
                k = j;
                m = a[1][j].second;
            }
        }
        if (k == -1) break;
        used[1][k] = true;
        x2 += m;
        ++ans;
        m = -1;
        k = -1;
        for (int j = 0; j < a[0].size (); ++j) {
            if (!used[0][j] && a[0][j].first <= x2 && a[0][j].second > m) {
                k = j;
                m = a[0][j].second;
            }
        }
        if (k == -1) break;
        used[0][k] = true;
        x2 += m;
        ++ans;
    }
    cout << max(ans, res);
}


#include <bits/stdc++.h>

using namespace std;

main () {
    int n, x;
    cin >> n >> x;
    multiset <pair <int, int> > a[2], b[2];
    for (int i = 0; i < n; ++i) {
        int t, h, m;
        cin >> t >> h >> m;
        a[t].insert ({h, m});
        a[t].insert ({h, m});
    }
    int x1 = x, x2 = x;
    int res = 0, ans = 0;
    for (;!a[0].empty () && !a[1].empty ();) {
        auto it = a[0].end ();
        int m = 0;
        for (auto y : a[0]) {
            if (y.first <= x1 && m < y.second) {
                it = a[0].lower_bound(y);
            }
        }
        if (it == a[0].end ()) break;
        ++res;
        x1 += m;
        a[0].erase (it);
        it = a[1].end ();
        m = 0;
        for (auto y : a[1]) {
            if (y.first <= x1 && m < y.second) {
                it = a[1].lower_bound(y);
            }
        }
        if (it == a[1].end ()) break;
        ++res;
        x1 += m;
        a[1].erase (it);
    }
    for (;!b[0].empty () && !b[1].empty ();) {
        auto it = b[0].end ();
        int m = 0;
        for (auto y : b[0]) {
            if (y.first <= x2 && m < y.second) {
                it = b[0].lower_bound(y);
            }
        }
        if (it == b[0].end ()) break;
        x2 += m;
        ++ans;
        b[0].erase (it);
        it = b[1].end ();
        m = 0;
        for (auto y : b[1]) {
            if (y.first <= x2 && m < y.second) {
                it = b[1].lower_bound(y);
            }
        }
        if (it == b[1].end ()) break;
        ++ans;
        x2 += m;
        b[1].erase (it);
    }
    cout << max(ans, res);
}


#include <bits/stdc++.h>

using namespace std;

main () {
    int n, x;
    cin >> n >> x;
    multiset <pair <int, int> > a[2], b[2];
    for (int i = 0; i < n; ++i) {
        int t, h, m;
        cin >> t >> h >> m;
        a[t].insert ({h, m});
        a[t].insert ({h, m});
    }
    int x1 = x, x2 = x;
    int res = 0, ans = 0;
    for (;!a[0].empty () && !a[1].empty ();) {
        auto it = a[0].end ();
        int m = 0;
        for (auto y : a[0]) {
            if (y.first <= x1 && m < y.second) {
                it = a[0].lower_bound(y);
                m = y.second;
            }
        }
        if (it == a[0].end ()) break;
        ++res;
        x1 += m;
        a[0].erase (it);
        it = a[1].end ();
        m = 0;
        for (auto y : a[1]) {
            if (y.first <= x1 && m < y.second) {
                it = a[1].lower_bound(y);
                m = y.second;
            }
        }
        if (it == a[1].end ()) break;
        ++res;
        x1 += m;
        a[1].erase (it);
    }
    for (;!b[0].empty () && !b[1].empty ();) {
        auto it = b[0].end ();
        int m = 0;
        for (auto y : b[0]) {
            if (y.first <= x2 && m < y.second) {
                it = b[0].lower_bound(y);
                m = y.second;
            }
        }
        if (it == b[0].end ()) break;
        x2 += m;
        ++ans;
        b[0].erase (it);
        it = b[1].end ();
        m = 0;
        for (auto y : b[1]) {
            if (y.first <= x2 && m < y.second) {
                it = b[1].lower_bound(y);
                m = y.second;
            }
        }
        if (it == b[1].end ()) break;
        ++ans;
        x2 += m;
        b[1].erase (it);
    }
    cout << max(ans, res);
}


#include <bits/stdc++.h>

using namespace std;

main () {
    int n, x;
    cin >> n >> x;
    vector <pair <int, int> > a[2];
    for (int i = 0; i < n; ++i) {
        int t, h, m;
        cin >> t >> h >> m;
        a[t].push_back ({h, -m});
    }
    sort (a[0].begin (), a[0].end ());
    sort (a[1].begin (), a[1].end ());
    int x1 = x, x2 = x;
    int res = 0, ans = 0;
    for (int i1 = 0, i2 = 0; i1 < a[0].size (), i2 < a[1].size (); ++i1, ++i2) {
        if (a[0][i1].first > x1) break;
        x1 -= a[0][i1].second;
        ++res;
        if (a[1][i2].first > x1) break;
        x1 -= a[1][i2].second;
        ++res;
    }
    for (int i1 = 0, i2 = 0; i1 < a[0].size (), i2 < a[1].size (); ++i1, ++i2) {
        if (a[1][i2].first > x2) break;
        x2 -= a[1][i2].second;
        ++ans;
        if (a[0][i1].first > x2) break;
        x2 -= a[0][i1].second;
        ++ans;
    }
    cout << max(ans, res);
}