import java.io.*;
import java.util.*;
import java.lang.*;
public class a
{
    public static void main(String[] args) throws java.io.IOException
    {
	BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
	int n=Integer.parseInt(a.readLine());
	String[] b=a.readLine().split(" ");
	int sum=0;
	for(int i=0;i<n;i++)
	    sum+=Integer.parseInt(b[i]);
	//System.out.println(sum);
	int m=Integer.parseInt(a.readLine());

	int output=-1;
	for(int j=0;j<m;j++)
	{
	    String[] c=a.readLine().split(" ");
	    int start=Integer.parseInt(c[0]);
	    int end=Integer.parseInt(c[1]);
	    if(start<=sum && end>=sum)
	    {
		output=sum;
		break;
	    }
	    if(start>sum)
	    {
		output=start;
		break;
	    }
	}
	System.out.println(output);
    }
}


import java.io.*;
import java.util.*;
import java.lang.*;
public class a
{
    public static void main(String[] args) throws java.io.IOException
    {
	BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
	int n=Integer.parseInt(a.readLine());
	String[] b=a.readLine().split(" ");
	int sum=0;
	for(int i=0;i<n;i++)
	    sum+=Integer.parseInt(b[i]);
	int m=Integer.parseInt(a.readLine());

	int output=-1;
	for(int j=0;j<m;j++)
	{
	    String[] c=a.readLine().split(" ");
	    int start=Integer.parseInt(c[0]);
	    int end=Integer.parseInt(c[1]);
	    if(start<=sum && end>=sum)
	    {
		output=sum;
		break;
	    }
	    if(start>sum)
		output=start;
	}
	System.out.println(output);
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Contest{

	public static void main(String[] args)throws IOException {
		Reader.init(System.in);
		int n=Reader.nextInt();
		int[] a=new int[n+1];
		int sum=0;
		for(int i=1;i<=n;i++){
			a[i]=Reader.nextInt();
			sum+=a[i];
		}
		int m=Reader.nextInt();
		int[] l=new int[m+1];
		int[] r=new int[m+1];
		for(int i=1;i<=m;i++){
			l[i]=Reader.nextInt();
			r[i]=Reader.nextInt();
		}

		if(sum>r[n])
			System.out.println(-1);
		else{
			int i;
			for(i=1;i<=m;i++){
				if(r[i]>=sum)
					break;
			}
			if(l[i]<=sum && r[i]>sum)
				System.out.println(sum);
			else
				System.out.println(r[i]);
		}
	}
}

/** Class for buffered reading int and double values */
class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /** call this method to initialize reader for InputStream */
    static void init(InputStream input) {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }

    /** get next word */
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }

    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Contest{

	public static void main(String[] args)throws IOException {
		Reader.init(System.in);
		int n=Reader.nextInt();
		int[] a=new int[n+1];
		int sum=0;
		for(int i=1;i<=n;i++){
			a[i]=Reader.nextInt();
			sum+=a[i];
		}
		int m=Reader.nextInt();
		int[] l=new int[m+1];
		int[] r=new int[m+1];
		for(int i=1;i<=m;i++){
			l[i]=Reader.nextInt();
			r[i]=Reader.nextInt();
		}

		if(sum>r[m])
			System.out.println(-1);
		else{
			int i;
			for(i=1;i<=m;i++){
				if(r[i]>=sum)
					break;
			}
			if(l[i]<=sum && r[i]>sum)
				System.out.println(sum);
			else if(l[i]>sum)
				System.out.println(l[i]);
			else
				System.out.println(r[i]);
		}
	}
}

/** Class for buffered reading int and double values */
class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /** call this method to initialize reader for InputStream */
    static void init(InputStream input) {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }

    /** get next word */
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }

    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Contest{

	public static void main(String[] args)throws IOException {
		Reader.init(System.in);
		int n=Reader.nextInt();
		int[] a=new int[n+1];
		int sum=0;
		for(int i=1;i<=n;i++){
			a[i]=Reader.nextInt();
			sum+=a[i];
		}
		int m=Reader.nextInt();
		int[] l=new int[m+1];
		int[] r=new int[m+1];
		for(int i=1;i<=m;i++){
			l[i]=Reader.nextInt();
			r[i]=Reader.nextInt();
		}

		if(sum>r[n])
			System.out.println(-1);
		else{
			int i;
			for(i=1;i<=n;i++){
				if(r[i]>=sum)
					break;
			}
			if(l[i]<=sum && r[i]>sum)
				System.out.println(sum);
			else
				System.out.println(r[i]);
		}
	}
}

/** Class for buffered reading int and double values */
class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /** call this method to initialize reader for InputStream */
    static void init(InputStream input) {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }

    /** get next word */
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }

    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
}


var n = parseInt(readline());
var q = Array(n);
var arr = readline().split(' ');
var total = 0;
for (var i = 0; i < n; i ++)
{
    q[i] = parseInt(arr[i]);
    total += q[i];
}
var m = parseInt(readline());
var t = Array(m);
var ret = -1;
for (var i = 0; i < m; i ++)
{
    var arr = readline().split(' ');
    t[i] = [parseInt(arr[0]), parseInt(arr[1])];
    if (total < t[i][0])
    {
        ret = t[i][0];
        break;
    }
    else if (total >= t[i][0] && total <= t[i][1])
    {
        ret = total;
        break;
    }
}
print(ret);


  var n = parseInt(readline());
  var a = readline().split(' ').map(x => parseInt(x));
  var tot = 0;
  a.forEach(x => tot += x);
  var m = parseInt(readline());
  var l = [];
  var r = [];
  var res = Infinity;
  for(var i = 0; i < m; i += 1) {
    var tmp = readline().split(' ').map(x => parseInt(x));
    var l = tmp[0];
    var r = tmp[1];
    if(tot >= l && tot <= r) {
      res = tot;
      break;
    }
    if(tot <= l) {
      res = l;
      break;
    }
  }
  print(res === Infinity ? -1 : res);


#include <iostream>

using namespace std;

int main()
{
    int n,m;//the number of problems,the number of time intervals
    int prob_time [1000];
    int intervals[1000][2];
    bool can = false;
    cin>>n;
    for(int i = 0;i<n;i++) cin>>prob_time[i];
    cin>>m;
    for(int i = 0;i<m;i++){
        cin>>intervals[i][0];
        cin>>intervals[i][1];
    }
    int total_time = 0;
    for(int i = 0;i<n;i++) total_time += prob_time[i];//calculate the required time for solving all the problems
    for(int i = 0;i<m&&!can;i++){
        if(total_time>=intervals[i][0] && total_time<=intervals[i][1]) can = true;
        if(total_time<intervals[i][0]){
            total_time = intervals[i][0];
            can = true;
        }
    }
    else cout<<-1;
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int n,m;//the number of problems,the number of time intervals
    int prob_time [1000];
    int intervals[1000][2];
    bool can = false;
    cin>>n;
    for(int i = 0;i<n;i++) cin>>prob_time[i];
    cin>>m;
    for(int i = 0;i<m;i++){
        cin>>intervals[i][0];
        cin>>intervals[i][1];
    }
    int total_time = 0;
    for(int i = 0;i<n;i++) total_time += prob_time[i];//calculate the required time for solving all the problems
    for(int i = 0;i<m&&!can;i++){
        if(total_time>=intervals[i][0] && total_time<=intervals[i][1]) can = true;
    }
    if(total_time<intervals[0][0])can = true;
    if(can) cout<<total_time;
    else cout<<-1;
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int n,m;//the number of problems,the number of time intervals
    int prob_time [1000];
    int intervals[1000][2];
    bool can = false;
    cin>>n;
    for(int i = 0;i<n;i++) cin>>prob_time[i];
    cin>>m;
    for(int i = 0;i<m;i++){
        cin>>intervals[i][0];
        cin>>intervals[i][1];
    }
    int total_time = 0;
    for(int i = 0;i<n;i++) total_time += prob_time[i];//calculate the required time for solving all the problems
    for(int i = 0;i<m&&!can;i++){
        if(total_time>=intervals[i][0] && total_time<=intervals[i][1]) can = true;
        if(total_time<intervals[i][0]){
            total_time = intervals[i][0];
            can = true;
        }
    }
    if(can) cout<<total_time;
    else cout<<-1;
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int n,m;//the number of problems,the number of time intervals
    int prob_time [1000];
    int intervals[1000][2];
    bool can = false;
    cin>>n;
    for(int i = 0;i<n;i++) cin>>prob_time[i];
    cin>>m;
    for(int i = 0;i<m;i++){
        cin>>intervals[i][0];
        cin>>intervals[i][1];
    }
    int total_time = 0;
    for(int i = 0;i<n;i++) total_time += prob_time[i];//calculate the required time for solving all the problems
    for(int i = 0;i<m;i++){
        if(total_time>=intervals[i][0] && total_time<=intervals[i][1]) can = true;
    }
    if(can) cout<<total_time;
    else cout<<-1;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool p[10000001];
int n,m;
int a[1000];
bool f;
int t;

int main(void)
{
	ios::sync_with_stdio(false);
	cin>>n;
	for (int i = 0; i < n; ++i)
	{
		cin>>a[i];
	}
	cin>>m;
	for (int i = 0; i < m; ++i)
	{
		int l,r;

		cin>>l>>r;
		for (int j = l; j <= min(100001,r); ++j)
		{
			p[j]=true;
		}
	}

	for (int i = 0; i < n; ++i)
	{
		t+=a[i];
		f=true;

		if(t>100001)
		{
			cout<<"-1";
			return 0;
		}
		if(p[t])
		{
			f=false;
		}
	}

	if(!f)
	{
		cout<<t;
	}
	else
	{
		for (int i = t; i < 100001; ++i)
		{
			if(p[i])
			{
				cout<<i;
				return 0;
			}
		}

		cout<<"-1";
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool p[10000001];
int n,m;
int a[1000];
bool f;
int t;

int main(void)
{
	ios::sync_with_stdio(false);
	cin>>n;
	for (int i = 0; i < n; ++i)
	{
		cin>>a[i];
	}
	cin>>m;
	for (int i = 0; i < m; ++i)
	{
		int l,r;

		cin>>l>>r;
		for (int j = l; j <= r; ++j)
		{
			p[j]=true;
		}
	}

	for (int i = 0; i < n; ++i)
	{
		t+=a[i];
		f=true;

		if(p[t])
		{
			f=false;
		}
	}

	if(!f)
	{
		cout<<t;
	}
	else
	{
		for (int i = t; i < 100001; ++i)
		{
			if(p[i])
			{
				cout<<i;
				return 0;
			}
		}

		cout<<"-1";
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool p[100001];
int n,m;
int a[1000];
bool f;
int t;

int main(void)
{
	//ios::sync_with_stdio(false);
	cin>>n;
	for (int i = 0; i < n; ++i)
	{
		cin>>a[i];
	}
	cin>>m;
	for (int i = 0; i < m; ++i)
	{
		int l,r;

		cin>>l>>r;
		for (int j = l; j <= r; ++j)
		{
			p[j]=true;
		}
	}

	for (int i = 0; i < n; ++i)
	{
		t+=a[i];
		f=true;

		if(p[t])
		{
			f=false;
		}
	}

	if(!f)
	{
		cout<<t;
	}
	else
	{
		for (int i = t; i < 100001; ++i)
		{
			if(p[i])
			{
				cout<<i;
				return 0;
			}
		}

		cout<<"-1";
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool p[1000001];
int n,m;
int a[1000];
bool f;
int t;

int main(void)
{
	//ios::sync_with_stdio(false);
	cin>>n;
	for (int i = 0; i < n; ++i)
	{
		cin>>a[i];
	}
	cin>>m;
	for (int i = 0; i < m; ++i)
	{
		int l,r;

		cin>>l>>r;
		for (int j = l; j <= r; ++j)
		{
			p[j]=true;
		}
	}

	for (int i = 0; i < n; ++i)
	{
		t+=a[i];
		f=true;

		if(p[t])
		{
			f=false;
		}
	}

	if(!f)
	{
		cout<<t;
	}
	else
	{
		for (int i = t; i < 100001; ++i)
		{
			if(p[i])
			{
				cout<<i;
				return 0;
			}
		}

		cout<<"-1";
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool p[10000001];
int n,m;
int a[1000];
bool f;
int t;

int main(void)
{
	ios::sync_with_stdio(false);
	cin>>n;
	for (int i = 0; i < n; ++i)
	{
		cin>>a[i];
	}
	cin>>m;
	for (int i = 0; i < m; ++i)
	{
		int l,r;

		cin>>l>>r;
		for (int j = l; j <= min(100001,r); ++j)
		{
			p[j]=true;
		}
	}

	for (int i = 0; i < n; ++i)
	{
		t+=a[i];
		f=true;

		if(p[t])
		{
			f=false;
		}
	}

	if(!f)
	{
		cout<<t;
	}
	else
	{
		for (int i = t; i < 100001; ++i)
		{
			if(p[i])
			{
				cout<<i;
				return 0;
			}
		}

		cout<<"-1";
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,a,s=0,x;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a),s+=a;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d%d",&x,&x);
	if(x<s)printf("-1");
	else printf("%d",s);
	return 0;
}


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

int n,m,a,s=0,l[1000],r[1000];

int search(int nl,int nr){
	if(nl==nr)return nl;
	int mid=(nl+nr)/2;
	if(r[mid]<s)return search(mid+1,nr);
	return search(nl,mid);
}

int main()
{

	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a),s+=a;
	scanf("%d",&m);
	for(int i=1;i<=m;i++)scanf("%d%d",&l[i],&r[i]);
	if(r[m]<s){
		printf("-1");
		return 0;
	}
	int ojbk=search(1,n);
	printf("%d",l[ojbk]<=s?s:l[ojbk]);
	return 0;
}


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

int n,m,a,s=0,l[1000],r[1000];

int search(int nl,int nr){
	if(nl==nr)return nl;
	int mid=(nl+nr)/2;
	if(r[mid]<s)return search(mid+1,nr);
	return search(nl,mid);
}

int main()
{

	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a),s+=a;
	scanf("%d",&m);
	for(int i=1;i<=m;i++)scanf("%d%d",&l[i],&r[i]);
	if(r[m]<s){
		printf("-1");
		return 0;
	}
	int ojbk=search(1,m);
	printf("%d",l[ojbk]<=s?s:l[ojbk]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,a,s=0,x=0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a),s+=a;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d%d",&x,&x);
	if(x<s)printf("-1");
	else printf("%d",s);
	return 0;
}


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

int n,m,a,s=0,l[1010],r[1010];

int search(int nl,int nr){
	if(nl==nr)return nl;
	int mid=(nl+nr)/2;
	if(r[mid]<s)return search(mid+1,nr);
	return search(nl,mid);
}

int main()
{

	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a),s+=a;
	scanf("%d",&m);
	for(int i=1;i<=m;i++)scanf("%d%d",&l[i],&r[i]);
	if(r[m]<s){
		printf("-1");
		return 0;
	}
	int ojbk=search(1,m);
	printf("%d",l[ojbk]<=s?s:l[ojbk]);
	return 0;
}


#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
priority_queue<int, vector<int>, greater<int> > q;
int a[1005];
int main()
{
    int n,m,x,y,b,temp=1,sum=0;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
        sum+=a[i];
    }
    sort(a,a+n);
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d%d",&x,&y);
        q.push(y-x+1);
    }
    for(int i=0; i<n; i++)
    {
        if(temp==0)
            break;
        while(1)
        {
            if(q.empty())
            {
                temp=0;
                break;
            }
            b=q.top();
            if(a[i]<b)
            {
                q.pop();
                q.push(b-a[i]);
                break;
            }
            else if(a[i]==b)
            {
                q.pop();
                break;
            }
            else
                q.pop();
        }

    }
    if(temp==0)
        printf("-1\n");
    else
        printf("%d\n",sum);
}


#include<cstdio>
int l[1005],r[1005];
int main()
{
    int n,m,x,sum=0;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&x);
        sum+=x;
    }
    scanf("%d",&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&l[i],&r[i]);
    }
    if(sum<l[0])
        printf("%d\n",l[0]);
    else if(sum>r[m-1])
        printf("-1\n");
    else
    {
        for(int i=0;i<m;i++)
        {
            if(sum>=l[i]&&sum<=r[i])
            {
                printf("%d\n",sum);
                break;
            }
            else if(sum<l[i])
            {
                printf("%d\n",l[i]);
                break;
            }
        }
    }
}


#include<cstdio>
int l[1005],r[1005];
int main()
{
    int n,m,x,sum=0;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&x);
        sum+=x;
    }
    scanf("%d",&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&l[i],&r[i]);
    }
    if(sum<l[0])
        printf("%d\n",l[0]);
    else if(sum>r[m-1])
        printf("-1\n");
    else
    {
        for(int i=0;i<m;i++)
        {
            if(sum>=l[i]&&sum<=r[i])
                printf("%d\n",sum);
            else if(sum<l[i])
                printf("%d\n",l[i]);
        }
    }
}


#include<cstdio>
int l[1005],r[1005];
int main()
{
    int n,m,x,sum=0;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&x);
        sum+=x;
    }
    scanf("%d",&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&l[i],&r[i]);
    }
    if(sum<l[0])
        printf("%d\n",l[0]);
    else if(sum>r[m-1])
        printf("-1\n");
    else
    {
        for(int i=0;i<m;i++)
        {
            if(sum>=l[i]&&sum<=r[i])
                printf("%d\n",sum);
        }
    }
}


#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
priority_queue<int, vector<int>, greater<int> > q;
int a[1005];
int main()
{
    int n,m,x,y,b,temp=1,sum=0;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
        sum+=a[i];
    }
    sort(a,a+n);
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d%d",&x,&y);
        q.push(y-x+1);
    }
    for(int i=0; i<n; i++)
    {
        if(temp==0)
            break;
        while(1)
        {
            if(q.empty())
            {
                temp=0;
                break;
            }
            b=q.top();
            if(a[i]<b)
            {
                q.pop();
                q.push(b-a[i]);
            }
            else if(a[i]==b)
            {
                q.pop();
                break;
            }
            else
                q.pop();
        }

    }
    if(temp==0)
        printf("-1\n");
    else
        printf("%d\n",sum);
}


#include<cstdio>
int l[1005],r[1005];
int main()
{
    int n,m,x,sum=0;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&x);
        sum+=x;
    }
    scanf("%d",&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&l[i],&r[i]);
    }
    if(sum<l[0])
        printf("%d\n",l[0]);
    else if(sum>r[m-1])
        printf("-1\n");
    else
    {
        for(int i=0;i<m;i++)
        {
            if(sum>=l[i]&&sum<=r[i])
                printf("%d\n",sum);
        }
        for(int i=0;i<m;i++)
        {
            if(sum<l[i])
                printf("%d\n",l[i]);
        }
    }
}


#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
priority_queue<int, vector<int>, greater<int> > q;
int a[1005];
int main()
{
    int n,m,x,y,b,temp=1,sum=0;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
        sum+=a[i];
    }
    sort(a,a+n-1);
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d%d",&x,&y);
        q.push(y-x+1);
    }
    for(int i=0; i<n; i++)
    {
        if(temp==0)
            break;
        while(1)
        {
            if(q.empty())
            {
                temp=0;
                break;
            }
            b=q.top();
            if(a[i]<b)
            {
                q.pop();
                q.push(b-a[i]);
                break;
            }
            else if(a[i]==b)
            {
                q.pop();
                break;
            }
            else
                q.pop();
        }

    }
    if(temp==0)
        printf("-1\n");
    else
        printf("%d\n",sum);
}


#include <iostream>
#include <utility>

using namespace std;

int main(){

    int total = 0;

    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++){
        cin >> a[i];
        total += a[i];
    }

    int m;
    cin >> m;
    pair<int, int> lr[m];
    for(int j = 0; j < m; j++){
        cin >> lr[j].first >> lr[j].second;

        if(total == lr[j].first || total == lr[j].second){
            cout << total;
            break;
        }
        else if(total > lr[j].first && total < lr[j].second){
            cout << total;
            break;
        }

    }

    if(lr[m - 1].second != 0){
        cout << "-1";
    }

}


#include <iostream>
#include <climits>

using namespace std;

int main(){

    int total = 0;
    int temp = INT_MAX;

    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++){
        cin >> a[i];
        total += a[i];
    }

    int m;
    cin >> m;
    int lr[m*2];
    for(int j = 0; j < m*2; j++){
        cin >> lr[j];

        if(total <= lr[j]){
            if(temp > lr[j]){
                temp = lr[j];
            }
        }

    }

    //cout << temp << endl;

    if(temp == INT_MAX){
        temp = -1;
    }

    cout << temp;

}


#include <iostream>
#include <utility>

using namespace std;

int main(){

    int total = 0;

    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++){
        cin >> a[i];
        total += a[i];
    }

    int m;
    cin >> m;
    pair<int, int> lr[m];
    for(int j = 0; j < m; j++){
        cin >> lr[j].first >> lr[j].second;
    }

    for(int j = 0; j < m; j++){
        if(total == lr[j].first || total == lr[j].second){
            cout << total;
            break;
        }
        else if(total > lr[j].first && total < lr[j].second){
            cout << total;
            break;
        }
        else if(lr[j + 1].first == 0){
            cout << "-1";
            break;
        }
    }

}


#include <iostream>
#include <utility>

using namespace std;

int main(){

    int total = 0;

    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++){
        cin >> a[i];
        total += a[i];
    }

    int m;
    cin >> m;
    pair<int, int> lr[m];
    for(int j = 0; j < m; j++){
        cin >> lr[j].first >> lr[j].second;

        if(total == lr[j].first || total == lr[j].second){
            cout << total;
            break;
        }
        else if(total > lr[j].first && total < lr[j].second){
            cout << total;
            break;
        }

    }

    if(total > lr[m - 2].second){
        cout << "-1";
    }

}


#include <iostream>
#include <utility>

using namespace std;

int main(){

    int total = 0;
    int temp = -1;

    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++){
        cin >> a[i];
        total += a[i];
    }

    int m;
    cin >> m;
    pair<int, int> lr[m];
    for(int j = 0; j < m; j++){
        cin >> lr[j].first >> lr[j].second;

        if(total == lr[j].first || total == lr[j].second){
            cout << total;
            temp = total;
            break;
        }
        else if(total > lr[j].first && total < lr[j].second){
            cout << total;
            temp = total;
            break;
        }

    }

    if(lr[m - 1].second != 0 && temp != total){
        cout << "-1";
    }

}


#include <iostream>
#include <utility>

using namespace std;

int main(){

    int total = 0;

    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++){
        cin >> a[i];
        total += a[i];
    }

    int m;
    cin >> m;
    pair<int, int> lr[m];
    for(int j = 0; j < m; j++){
        cin >> lr[j].first >> lr[j].second;
    }

    for(int j = 0; j < m; j++){
        if((total == lr[j].first || total == lr[j].second) || (total > lr[j].first && total < lr[j].second)){
            cout << total;
            break;
        }
    }

    if(total > lr[m - 1].second){
        cout << "-1";
    }

}


#include <iostream>
#include <utility>

using namespace std;

int main(){

    int total = 0;

    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++){
        cin >> a[i];
        total += a[i];
    }

    int m;
    cin >> m;
    pair<int, int> lr[m];
    for(int j = 0; j < m; j++){
        cin >> lr[j].first >> lr[j].second;
    }

    for(int j = 0; j < m; j++){
        if((total == lr[j].first || total == lr[j].second) || (total > lr[j].first && total < lr[j].second)){
            cout << total;
            break;
        }
        else if(total < lr[j].first){
            cout << lr[j].first;
            break;
        }
    }

    if(total > lr[m - 1].second || m == 0){
        cout << "-1";
    }

}


#include <iostream>
#include <utility>

using namespace std;

int main(){

    int total = 0;

    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++){
        cin >> a[i];
        total += a[i];
    }

    int m;
    cin >> m;
    pair<int, int> lr[m];
    for(int j = 0; j < m; j++){
        cin >> lr[j].first >> lr[j].second;

        if(total == lr[j].first || total == lr[j].second){
            cout << total;
            break;
        }
        else if(total > lr[j].first && total < lr[j].second){
            cout << total;
            break;
        }

    }

    if(total > lr[m - 1].second){
        cout << "-1";
    }

}


#include <iostream>
#include <utility>

using namespace std;

int main(){

    int total = 0;

    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++){
        cin >> a[i];
        total += a[i];
    }

    int m;
    cin >> m;
    pair<int, int> lr[m];
    for(int j = 0; j < m; j++){
        cin >> lr[j].first >> lr[j].second;
    }

    for(int j = 0; j < m; j++){
        if((total == lr[j].first || total == lr[j].second) || (total > lr[j].first && total < lr[j].second)){
            cout << total;
            break;
        }
        else if(total < lr[j].first){
            cout << lr[j].first;
            break;
        }
    }

    if(total > lr[m - 1].second){
        cout << "-1";
    }

}


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

int timee[10000001];
int main()
{
    int m,n;
    long long int all,a,b;
    while(cin>>n)
    {
        long long int ans=0;
        memset(timee,0,sizeof(timee));
        all=0;
        for(int i=0;i<n;i++)
        {
            cin>>a;
            all+=a;
        }
        //cout<<all<<endl;
        cin>>m;
        long long int maxx=0;
        for(int i=0;i<m;i++)
        {
            cin>>a>>b;
            if(b>maxx)
                maxx=b;

            for(int i=a;i<=b;i++)
                timee[i]=1;
        }
        int flag=0;
        //cout<<timee[all]<<endl;
        if(timee[all]==0)
        {
            for(int i=all;i<=maxx+1;i++)
            {
                if(timee[i]==1)
                {
                    flag=1;
                    ans=i;
                    break;
                }
            }
        }
        else
        {
            for(int i=all;i>=0;i--)
            {
                //cout<<"2222222"<<endl;
                if(timee[i]==0)
                {
                    ans=i+1;
                    flag=1;
                }
                if(ans<all)
                    ans=all;
                if(flag==1)
                    break;
            }
        }
        if(flag==1)
            cout<<ans<<endl;
        else
            cout<<"-1"<<endl;
    }
    return 0;
}


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

struct node
{
    int start;
    int endd;
}p[1005];

int main()
{
    int m,n;
    int num[1005];
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
            cin>>num[i];
        cin>>m;
        for(int i=0;i<m;i++)
        {
            cin>>p[i].start>>p[i].endd;
        }
        int all=0;
        int ans=0;
        int flag=0;
        for(int i=0;i<n;i++)
        {
            all=num[i]+all;
        }
        for(int i=0;i<m;i++)
        {
            if(all<=p[i].endd&&p[i].start>=all)
            {
                ans=p[i].start;
                flag=1;
            }
            if(all<=p[i].endd&&p[i].start<all)
            {
                ans=all;
                flag=1;
            }
            if(flag==1)
                break;
        }
        if(flag==1)
        cout<<ans<<endl;
        else
            cout<<"-1"<<endl;
    }
    return 0;
}


#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    int timee[10005];
    int m,n;
    int all,a,b;
    while(cin>>n)
    {
        int ans=0;
        memset(timee,0,sizeof(timee));
        all=0;
        for(int i=0;i<n;i++)
        {
            cin>>a;
            all+=a;
        }
        //cout<<all<<endl;
        cin>>m;
        for(int i=1;i<=m;i++)
        {
            cin>>a>>b;
            for(int i=a;i<=b;i++)
                timee[i]=1;
        }
        int flag=0;
        //cout<<timee[all]<<endl;
        if(timee[all]==0)
        {
            for(int i=all;i<=10001;i++)
            {
                if(timee[i]==1)
                {
                    flag=1;
                    ans=i;
                    break;
                }
            }
        }
        else
        {
            for(int i=all;i>=0;i--)
            {
                //cout<<"2222222"<<endl;
                if(timee[i]==0)
                {
                    ans=i+1;
                    flag=1;
                }
                if(ans<all)
                    ans=all;
            }
        }
        if(flag==1)
        cout<<ans<<endl;
        else
            cout<<"-1"<<endl;
    }
}


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

int timee[100000009];
int main()
{
    int m,n;
    long long int all,a,b;
    while(cin>>n)
    {
        long long int ans=0;
        memset(timee,0,sizeof(timee));
        all=0;
        for(int i=0;i<n;i++)
        {
            cin>>a;
            all+=a;
        }
        //cout<<all<<endl;
        cin>>m;
        long long int maxx=0;
        for(int i=0;i<m;i++)
        {
            cin>>a>>b;
            if(b>maxx)
                maxx=b;

            for(int i=a;i<=b;i++)
                timee[i]=1;
        }
        int flag=0;
        //cout<<timee[all]<<endl;
        if(timee[all]==0)
        {
            for(int i=all;i<=maxx+10000;i++)
            {
                if(timee[i]==1)
                {
                    flag=1;
                    ans=i;
                    break;
                }
            }
        }
        else
        {
            for(int i=all;i>=0;i--)
            {
                //cout<<"2222222"<<endl;
                if(timee[i]==0)
                {
                    ans=i+1;
                    flag=1;
                }
                if(ans<all)
                    ans=all;
                if(flag==1)
                    break;
            }
        }
        if(flag==1)
            cout<<ans<<endl;
        else
            cout<<"-1"<<endl;
    }
    return 0;
}


#include <iostream>
#include <string.h>
using namespace std;
    int timee[1000000];
int main()
{
    int m,n;
    long long int all,a,b;
    while(cin>>n)
    {
        int ans=0;
        memset(timee,0,sizeof(timee));
        all=0;
        for(int i=0;i<n;i++)
        {
            cin>>a;
            all+=a;
        }
        //cout<<all<<endl;
        cin>>m;
        int maxx=0;
        for(int i=0;i<m;i++)
        {
            cin>>a>>b;
            if(b>maxx)
                maxx=b;
            for(int i=a;i<=b;i++)
                timee[i]=1;
        }
        int flag=0;
        //cout<<timee[all]<<endl;
        if(timee[all]==0)
        {
            for(int i=all;i<=maxx+1;i++)
            {
                if(timee[i]==1)
                {
                    flag=1;
                    ans=i;
                    break;
                }
            }
        }
        else
        {
            for(int i=all;i>=0;i--)
            {
                //cout<<"2222222"<<endl;
                if(timee[i]==0)
                {
                    ans=i+1;
                    flag=1;
                }
                if(ans<all)
                    ans=all;
                if(flag==1)
                    break;
            }
        }
        if(flag==1)
        cout<<ans<<endl;
        else
            cout<<"-1"<<endl;
    }
}


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

int timee[100000001];
int main()
{
    int m,n;
    long long int all,a,b;
    while(cin>>n)
    {
        long long int ans=0;
        memset(timee,0,sizeof(timee));
        all=0;
        for(int i=0;i<n;i++)
        {
            cin>>a;
            all+=a;
        }
        //cout<<all<<endl;
        cin>>m;
        long long int maxx=0;
        for(int i=0;i<m;i++)
        {
            cin>>a>>b;
            if(b>maxx)
                maxx=b;

            for(int i=a;i<=b;i++)
                timee[i]=1;
        }
        int flag=0;
        //cout<<timee[all]<<endl;
        if(timee[all]==0)
        {
            for(int i=all;i<=maxx+1;i++)
            {
                if(timee[i]==1)
                {
                    flag=1;
                    ans=i;
                    break;
                }
            }
        }
        else
        {
            for(int i=all;i>=0;i--)
            {
                //cout<<"2222222"<<endl;
                if(timee[i]==0)
                {
                    ans=i+1;
                    flag=1;
                }
                if(ans<all)
                    ans=all;
                if(flag==1)
                    break;
            }
        }
        if(flag==1)
            cout<<ans<<endl;
        else
            cout<<"-1"<<endl;
    }
    return 0;
}


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

int timee[10000009];
int main()
{
    int m,n;
    long long int all,a,b;
    while(cin>>n)
    {
        long long int ans=0;
        memset(timee,0,sizeof(timee));
        all=0;
        for(int i=0;i<n;i++)
        {
            cin>>a;
            all+=a;
        }
        //cout<<all<<endl;
        cin>>m;
        long long int maxx=0;
        for(int i=0;i<m;i++)
        {
            cin>>a>>b;
            if(b>maxx)
                maxx=b;

            for(int i=a;i<=b;i++)
                timee[i]=1;
        }
        int flag=0;
        //cout<<timee[all]<<endl;
        if(timee[all]==0)
        {
            for(int i=all;i<=maxx+100;i++)
            {
                if(timee[i]==1)
                {
                    flag=1;
                    ans=i;
                    break;
                }
            }
        }
        else
        {
            for(int i=all;i>=0;i--)
            {
                //cout<<"2222222"<<endl;
                if(timee[i]==0)
                {
                    ans=i+1;
                    flag=1;
                }
                if(ans<all)
                    ans=all;
                if(flag==1)
                    break;
            }
        }
        if(flag==1)
            cout<<ans<<endl;
        else
            cout<<"-1"<<endl;
    }
    return 0;
}


#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    int timee[100005];
    int m,n;
    long long int all,a,b;
    while(cin>>n)
    {
        int ans=0;
        memset(timee,0,sizeof(timee));
        all=0;
        for(int i=0;i<n;i++)
        {
            cin>>a;
            all+=a;
        }
        //cout<<all<<endl;
        cin>>m;
        for(int i=1;i<=m;i++)
        {
            cin>>a>>b;
            for(int i=a;i<=b;i++)
                timee[i]=1;
        }
        int flag=0;
        //cout<<timee[all]<<endl;
        if(timee[all]==0)
        {
            for(int i=all;i<=1111;i++)
            {
                if(timee[i]==1)
                {
                    flag=1;
                    ans=i;
                    break;
                }
            }
        }
        else
        {
            for(int i=all;i>=0;i--)
            {
                //cout<<"2222222"<<endl;
                if(timee[i]==0)
                {
                    ans=i+1;
                    flag=1;
                }
                if(ans<all)
                    ans=all;
            }
        }
        if(flag==1)
        cout<<ans<<endl;
        else
            cout<<"-1"<<endl;
    }
}


#include <iostream>
#include <string.h>
using namespace std;
    int timee[1000000];
int main()
{
    int m,n;
    long long int all,a,b;
    while(cin>>n)
    {
        int ans=0;
        memset(timee,0,sizeof(timee));
        all=0;
        for(int i=0;i<n;i++)
        {
            cin>>a;
            all+=a;
        }
        //cout<<all<<endl;
        cin>>m;
        for(int i=0;i<m;i++)
        {
            cin>>a>>b;
            for(int i=a;i<=b;i++)
                timee[i]=1;
        }
        int flag=0;
        //cout<<timee[all]<<endl;
        if(timee[all]==0)
        {
            for(int i=all;i<=100005;i++)
            {
                if(timee[i]==1)
                {
                    flag=1;
                    ans=i;
                    break;
                }
            }
        }
        else
        {
            for(int i=all;i>=0;i--)
            {
                //cout<<"2222222"<<endl;
                if(timee[i]==0)
                {
                    ans=i+1;
                    flag=1;
                }
                if(ans<all)
                    ans=all;
            }
        }
        if(flag==1)
        cout<<ans<<endl;
        else
            cout<<"-1"<<endl;
    }
}


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

int timee[100000001];
int main()
{
    int m,n;
    int all,a,b;
    while(cin>>n)
    {
        int ans=0;
        memset(timee,0,sizeof(timee));
        all=0;
        for(int i=0;i<n;i++)
        {
            cin>>a;
            all+=a;
        }
        //cout<<all<<endl;
        cin>>m;
        int maxx=0;
        for(int i=0;i<m;i++)
        {
            cin>>a>>b;
            if(b>maxx)
                maxx=b;

            for(int i=a;i<=b;i++)
                timee[i]=1;
        }
        int flag=0;
        //cout<<timee[all]<<endl;
        if(timee[all]==0)
        {
            for(int i=all;i<=maxx+1;i++)
            {
                if(timee[i]==1)
                {
                    flag=1;
                    ans=i;
                    break;
                }
            }
        }
        else
        {
            for(int i=all;i>=0;i--)
            {
                //cout<<"2222222"<<endl;
                if(timee[i]==0)
                {
                    ans=i+1;
                    flag=1;
                }
                if(ans<all)
                    ans=all;
                if(flag==1)
                    break;
            }
        }
        if(flag==1)
            cout<<ans<<endl;
        else
            cout<<"-1"<<endl;
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;
int n,m,sum,flag;
int a[100005];
int b[100005];
int c[100005];
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        flag=sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&b[i],&c[i]);
            if(c[i]>=sum)
            {
                printf("%d\n",max(sum,b[i]));
                flag=1;
                break;
            }
        }
        if(!flag)
            printf("-1\n");
    }
    return 0;
}


#include <cstdio>
using namespace std;
int n,m,sum,flag;
int a[1005];
int b[2005];
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        flag=sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        scanf("%d",&m);
        for(int i=0;i<2*m;i++)
        {
            scanf("%d",&b[i]);
            if(i%2&&b[i]>=sum)
            {
                printf("%d\n",sum);
                flag=1;
                break;
            }
        }
        if(!flag)
            printf("-1\n");
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
int n,m,flag;
ll sum;
ll a[100005];
ll b[200005];
int main()
{
    while(cin>>n)
    {
        flag=sum=0;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            sum+=a[i];
        }
        scanf("%d",&m);
        for(int i=0;i<2*m;i++)
        {
            cin>>b[i];
            if(i%2&&b[i]>=sum)
            {

                cout<<max(sum,b[i-1])<<endl;
                flag=1;
                break;
            }
        }
        if(!flag)
            cout<<"-1"<<endl;
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;
int n,m,sum,flag;
int a[1005];
int b[2005];
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        flag=sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        scanf("%d",&m);
        for(int i=0;i<2*m;i++)
        {
            scanf("%d",&b[i]);
            if(i%2&&b[i]>=sum)
            {
                printf("%d\n",max(sum,b[i-1]));
                flag=1;
                break;
            }
        }
        if(!flag)
            printf("-1\n");
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;
int n,m,sum,flag;
int a[1005];
int b[2005];
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        flag=sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        scanf("%d",&m);
        for(int i=0;i<2*m;i++)
        {
            scanf("%d",&b[i]);
            if(i%2&&b[i]>=sum)
            {
                printf("%d\n",max(sum,b[i-1]);
                flag=1;
                break;
            }
        }
        if(!flag)
            printf("-1\n");
    }
    return 0;
}


#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
int n,m,flag;
ll sum;
ll a[100005];
ll b[200005];
int main()
{
    while(cin>>n)
    {
        flag=sum=0;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            sum+=a[i];
        }
        scanf("%d",&m);
        for(int i=0;i<2*m;i++)
        {
            cin>>b[i];
            if(i%2&&b[i]>=sum)
            {
                cout<<sum<<endl;
                flag=1;
                break;
            }
        }
        if(!flag)
            cout<<"-1"<<endl;
    }
    return 0;
}


#include<cstdio>
#include<cstring>

using namespace std;

typedef struct Node
{
    int left,right;
}node;

node time[100005];
int need[100005];

int main()
{
    int n,k;
    int flag = 0;
    while(scanf("%d", &n) != EOF)
    {
        int i;
        int sum =0;
        for(i = 0; i < n ;i++)
        {
            scanf("%d", &need[i]);
            sum+=need[i];
        }
        scanf("%d", &k);
        for(i = 0; i < k; i++)
        {
            scanf("%d%d", &time[i].left,&time[i].right);
            if(time[i].right >= sum && sum >= time[i].left)
            {
                printf("%d\n",sum);
                flag = 1;
                break;
            }
            else if(sum <= time[i].left)
            {
                printf("%d\n",time[i].left);
                flag = 1;
                break;
            }
        }
        if(!flag)
            printf("-1\n");
    }
    return 0;
}


#include <cstdio>
using namespace std;
int n,m,sum,flag;
int a[100005];
int b[200005];
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        flag=sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        scanf("%d",&m);
        for(int i=0;i<2*m;i++)
        {
            scanf("%d",&b[i]);
            if(i%2&&b[i]>=sum)
            {
                printf("%d\n",sum);
                flag=1;
                break;
            }
        }
        if(!flag)
            printf("-1\n");
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;
int n,m,sum,flag;
int a[100005];
int b[200005];
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        flag=sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        scanf("%d",&m);
        for(int i=0;i<2*m;i++)
        {
            scanf("%d",&b[i]);
            if(i%2&&b[i]>=sum)
            {
                printf("%d\n",max(sum,b[i-1]));
                flag=1;
                break;
            }
        }
        if(!flag)
            printf("-1\n");
    }
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;
long long n;
int a[100010];
int b[100010];
int main()
{
 //freopen("input.txt","r",stdin);
 //freopen("output.txt","w",stdout);
 ios_base::sync_with_stdio(0);
 int n,m,i;
 long long ans=0;
  cin>>n;
  for (int i=0;i<n;i++)
  {
      cin>>m;
      ans+=m;
  }
 cin>>m;
 for (int i=0;i<m;i++)
 {int l,r;
   cin>>l>>r;
   if (ans>=l) {cout<<ans;return 0;}
 }
 cout<<-1;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;
long long n;
int a[100010];
int b[100010];
int main()
{
 //freopen("input.txt","r",stdin);
 //freopen("output.txt","w",stdout);
 ios_base::sync_with_stdio(0);
 int n,m,i;
 long long ans=0;
  cin>>n;
  for (int i=0;i<n;i++)
  {
      cin>>m;
      ans+=m;
  }
 cin>>m;
 for (int i=0;i<n;i++)
 {int l,r;
   cin>>l>>r;
   if (ans>=l && ans<=r) {cout<<ans;return 0;}
 }
 cout<<-1;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;
long long n;
int a[100010];
int b[100010];
int main()
{
 //freopen("input.txt","r",stdin);
 //freopen("output.txt","w",stdout);
 ios_base::sync_with_stdio(0);
 int n,m,i;
 long long ans=0;
  cin>>n;
  for (int i=0;i<n;i++)
  {
      cin>>m;
      ans+=m;
  }
 cin>>m;
 for (int i=0;i<m;i++)
 {int l,r;
   cin>>l>>r;
   if (ans>=l && ans<=r) {cout<<ans;return 0;}
   if (ans<=l) {cout<<ans;return 0;}
 }
 cout<<-1;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;
long long n;
int a[100010];
int b[100010];
int main()
{
 //freopen("input.txt","r",stdin);
 //freopen("output.txt","w",stdout);
 ios_base::sync_with_stdio(0);
 int n,m,i;
 long long ans=0;
  cin>>n;
  for (int i=0;i<n;i++)
  {
      cin>>m;
      ans+=m;
  }
 cin>>m;
 for (int i=0;i<m;i++)
 {int l,r;
   cin>>l>>r;
   if (ans<=r) {cout<<ans;return 0;}
 }
 cout<<-1;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;
long long n;
int a[100010];
int b[100010];
int main()
{
 //freopen("input.txt","r",stdin);
 //freopen("output.txt","w",stdout);
 ios_base::sync_with_stdio(0);
 int n,m,i;
 long long ans=0;
  cin>>n;
  for (int i=0;i<n;i++)
  {
      cin>>m;
      ans+=m;
  }
 cin>>m;
 for (int i=0;i<m;i++)
 {int l,r;
   cin>>l>>r;
   if (ans>=l && ans<=r) {cout<<ans;return 0;}
   if (ans<=l || ans<=r) {cout<<ans;return 0;}
 }
 cout<<-1;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;
long long n;
int a[100010];
int b[100010];
int main()
{
 //freopen("input.txt","r",stdin);
 //freopen("output.txt","w",stdout);
 ios_base::sync_with_stdio(0);
 int n,m,i;
 long long ans=0;
  cin>>n;
  for (int i=0;i<n;i++)
  {
      cin>>m;
      ans+=m;
  }
 cin>>m;
 for (int i=0;i<m;i++)
 {int l,r;
   cin>>l>>r;
   if (ans>=l && ans<=r) {cout<<ans;return 0;}
 }
 cout<<-1;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;
long long n;
int a[100010];
int b[100010];
int main()
{
 //freopen("input.txt","r",stdin);
 //freopen("output.txt","w",stdout);
 ios_base::sync_with_stdio(0);
 int n,m,i;
 long long ans=0;
  cin>>n;
  for (int i=0;i<n;i++)
  {
      cin>>m;
      ans+=m;
  }
 cin>>m;
 for (int i=0;i<m;i++)
 {long long l,r;
   cin>>l>>r;
   if (ans<=r) {cout<<max(ans,l);return 0;}
 }
 cout<<-1;
}


#include <bits/stdc++.h>

#define rust(a, b, c, d) sqrt(sqr(a - c) + sqr(b - d))
#define sqr(a) (a)*(a)
#define m_p make_pair
#define fi first
#define se second
#define fast_io ios_base::sync_with_stdio(0);cin.tie(0);
#define endl "\n"

typedef long long ll;
const int MAXINT=2147483640;
const ll MAXLL=9223372036854775800LL;
const ll MAXN=1000000;
using namespace std;
pair <ll,ll> b[20000];
ll k,z,n,m,a[20000],mx1=-1e9,mx2=-1e9,i,j,l,r;
int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);
    fast_io;
    cin >> n;
    for (i=1; i<=n; i++) {cin >> a[i]; k+=a[i];}
    cin >> m;
    for (i=1; i<=m; i++)
    {
        cin >> b[i].fi >> b[i].se;
    }
    for (i=1; i<=m; i++)
    {
        if (b[i].fi<=k && b[i].se>=k)
        {
            cout << k << endl;
            return 0;
        }

    }
    for (i=1; i<=m; i++)
    {
        if (k<=b[i].fi)
        {
            cout << b[i].fi;
            return 0;
        }
        if (k<=b[i].se)
        {
            cout << b[i].se;
            return 0;
        }
    }
    cout << -1;

}


#include <bits/stdc++.h>

#define rust(a, b, c, d) sqrt(sqr(a - c) + sqr(b - d))
#define sqr(a) (a)*(a)
#define m_p make_pair
#define fi first
#define se second
#define fast_io ios_base::sync_with_stdio(0);cin.tie(0);
#define endl "\n"

typedef long long ll;
const int MAXINT=2147483640;
const ll MAXLL=9223372036854775800LL;
const ll MAXN=1000000;
using namespace std;
pair <ll,ll> b[1412142];
ll k,z,n,m,a[4212412],c[31213123],mx1=-1e9,mx2=-1e9,i,j,l,r;
int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);
    fast_io;
    cin >> n;
    for (i=1; i<=n; i++) {cin >> a[i]; k+=a[i];}
    cin >> m;
    for (i=1; i<=m; i++)
    {
        cin >> b[i].fi >> b[i].se;
    }
    for (i=1; i<=m; i++)
    {
        if (k<=b[i].fi)
        {
            cout << b[i].fi;
            return 0;
        }
        if (k<=b[i].se)
        {
            cout << b[i].se;
            return 0;
        }
    }
    cout << -1;

}


#include <bits/stdc++.h>

#define rust(a, b, c, d) sqrt(sqr(a - c) + sqr(b - d))
#define sqr(a) (a)*(a)
#define m_p make_pair
#define fi first
#define se second
#define fast_io ios_base::sync_with_stdio(0);cin.tie(0);
#define endl "\n"

typedef long long ll;
const int MAXINT=2147483640;
const ll MAXLL=9223372036854775800LL;
const ll MAXN=1000000;
using namespace std;
ll k,z,n,m,a[4212412],b[1412441],mx=-1e9,i,j,l,r;
int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);
    fast_io;
    cin >> n;
    for (i=1; i<=n; i++) {cin >> a[i]; k+=a[i];}
    cin >> m;
    for (i=1; i<=m; i++)
    {
        cin >> l >> r;
     //   b[i]=r-l;
        if (l>mx) mx=l;
        if (r>mx) mx=r;
    }
    if (mx>=k) cout << k << endl;
    else cout << -1 << endl;

}


#include <bits/stdc++.h>

#define rust(a, b, c, d) sqrt(sqr(a - c) + sqr(b - d))
#define sqr(a) (a)*(a)
#define m_p make_pair
#define fi first
#define se second
#define fast_io ios_base::sync_with_stdio(0);cin.tie(0);
#define endl "\n"

typedef long long ll;
const int MAXINT=2147483640;
const ll MAXLL=9223372036854775800LL;
const ll MAXN=1000000;
using namespace std;
ll k,z,n,m,a[4212412],b[1412441],mx1=-1e9,mx2=-1e9,i,j,l,r;
int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);
    fast_io;
    cin >> n;
    for (i=1; i<=n; i++) {cin >> a[i]; k+=a[i];}
    cin >> m;
    for (i=1; i<=m; i++)
    {
        cin >> l >> r;
        if (mx1<r) mx1=r;
        if (mx2<l) mx2=l;

    }
    //cout << mx1 << " "<< mx2;
    if (k>=mx2 && k<=mx1) cout << k << endl;
    else cout << -1;

}


#include <bits/stdc++.h>

#define rust(a, b, c, d) sqrt(sqr(a - c) + sqr(b - d))
#define sqr(a) (a)*(a)
#define m_p make_pair
#define fi first
#define se second
#define fast_io ios_base::sync_with_stdio(0);cin.tie(0);
#define endl "\n"

typedef long long ll;
const int MAXINT=2147483640;
const ll MAXLL=9223372036854775800LL;
const ll MAXN=1000000;
using namespace std;
pair <ll,ll> b[20000];
ll k,z,n,m,a[20000],mx1=-1e9,mx2=-1e9,i,j,l,r;
int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);
    fast_io;
    cin >> n;
    for (i=1; i<=n; i++) {cin >> a[i]; k+=a[i];}
    cin >> m;
    for (i=1; i<=m; i++)
    {
        cin >> b[i].fi >> b[i].se;
        if (b[i])
    }
    for (i=1; i<=m; i++)
    {
        if (b[i].fi<=k && b[i].se>=k)
        {
            cout << k << endl;
            return 0;
        }

    }
    for (i=1; i<=m; i++)
    {
        if (k<=b[i].fi)
        {
            cout << b[i].fi;
            return 0;
        }
        if (k<=b[i].se)
        {
            cout << b[i].se;
            return 0;
        }
    }
    cout << -1;

}


#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size()

int main() {
	int usb, ps2, both;
	cin >> usb >> ps2 >> both;
	int idx = 0;
	int M;
	cin >> M;
	int result = 0;
	vector<pair<int, int>> mice(M);
	for (int i = 0;i < M;i++) {
		int val;
		string port;
		cin >> val >> port;
		mice[i].first = val;
		if (port == "USB") mice[i].second = 1;
		else mice[i].second = 2;
	}
	sort(begin(mice), end(mice));
	while (usb > 0 and ps2 > 0 and both > 0 and idx < M) {
		int type = mice[idx].second;
		int price = mice[idx].first;
		if (type == 1 and usb > 0) {
			usb -= 1;
			result += price;
			idx++;
		}
		else if (type == 1 and both > 0) {
			both -= 1;
			result += price;
			idx++;
		}
		else if (type == 2 and ps2 > 0) {
			ps2 -= 1;
			result += price;
			idx++;
		}
		else if (type == 2 and both > 0) {
			both -= 1;
			result += price;
			idx++;
		}
		else idx++;
	}
	cout << M << " " << result;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size()
typedef long long ll;

int main() {
	int usb, ps2, both;
	cin >> usb >> ps2 >> both;
	int idx = 0;
	int M;
	cin >> M;
	ll result = 0;
	ll result2 = 0;
	vector<pair<ll, ll>> mice(M);
	for (int i = 0;i < M;i++) {
		ll val;
		string port;
		cin >> val >> port;
		mice[i].first = val;
		if (port == "USB") mice[i].second = 1;
		else mice[i].second = 2;
	}
	sort(begin(mice), end(mice));
	while ((usb > 0 or ps2 > 0 or both > 0) and idx < M) {
		ll type = mice[idx].second;
		ll price = mice[idx].first;
		if (type == 1 and usb > 0) {
			usb -= 1;
			result += price;
			result2++;
			idx++;
		}
		else if (type == 1 and both > 0) {
			both -= 1;
			result += price;
			result2++;
			idx++;
		}
		else if (type == 2 and ps2 > 0) {
			ps2 -= 1;
			result += price;
			result2++;
			idx++;
		}
		else if (type == 2 and both > 0) {
			both -= 1;
			result += price;
			result2++;
			idx++;
		}
		else idx++;
	}
	cout << result2 << " " << result;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size()

int main() {
	int usb, ps2, both;
	cin >> usb >> ps2 >> both;
	int idx = 0;
	int M;
	cin >> M;
	int result = 0;
	int result2 = 0;
	vector<pair<int, int>> mice(M);
	for (int i = 0;i < M;i++) {
		int val;
		string port;
		cin >> val >> port;
		mice[i].first = val;
		if (port == "USB") mice[i].second = 1;
		else mice[i].second = 2;
	}
	sort(begin(mice), end(mice));
	while ((usb > 0 or ps2 > 0 or both > 0) and idx < M) {
		int type = mice[idx].second;
		int price = mice[idx].first;
		if (type == 1 and usb > 0) {
			usb -= 1;
			result += price;
			result2++;
			idx++;
		}
		else if (type == 1 and both > 0) {
			both -= 1;
			result += price;
			result2++;
			idx++;
		}
		else if (type == 2 and ps2 > 0) {
			ps2 -= 1;
			result += price;
			result2++;
			idx++;
		}
		else if (type == 2 and both > 0) {
			both -= 1;
			result += price;
			result2++;
			idx++;
		}
		else idx++;
	}
	cout << result2 << " " << result;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size()

int main() {
	int usb, ps2, both;
	cin >> usb >> ps2 >> both;
	int idx = 0;
	int M;
	cin >> M;
	int result = 0;
	int result2 = 0;
	vector<pair<int, int>> mice(M);
	for (int i = 0;i < M;i++) {
		int val;
		string port;
		cin >> val >> port;
		mice[i].first = val;
		if (port == "USB") mice[i].second = 1;
		else mice[i].second = 2;
	}
	sort(begin(mice), end(mice));
	while (usb > 0 and ps2 > 0 and both > 0 and idx < M) {
		int type = mice[idx].second;
		int price = mice[idx].first;
		if (type == 1 and usb > 0) {
			usb -= 1;
			result += price;
			result2++;
			idx++;
		}
		else if (type == 1 and both > 0) {
			both -= 1;
			result += price;
			result2++;
			idx++;
		}
		else if (type == 2 and ps2 > 0) {
			ps2 -= 1;
			result += price;
			result2++;
			idx++;
		}
		else if (type == 2 and both > 0) {
			both -= 1;
			result += price;
			result2++;
			idx++;
		}
		else idx++;
	}
	cout << result2 << " " << result;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define IN freopen("in.txt", "r", stdin);
#define OUT freopen("out.txt","w",stdout);
#define endl '\n'
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define int long long
#define sz(x) (int)x.size()
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const ll inf = 0x3f3f3f3f3f3f3f3f;

const int MAXN = 1e5+1000;
const int MAXM = 1e5+1000;
struct Edge{
    int to,next,cap,flow,cost;
}e[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;
void init(int n){
    N=n;
    tol=0;
    memset(head,-1,sizeof head);
}
void addedge(int u,int v,int cap,int cost){
    e[tol].to=v;
    e[tol].cap=cap;
    e[tol].cost=cost;
    e[tol].flow=0;
    e[tol].next=head[u];
    head[u]=tol++;
    e[tol].to=u;
    e[tol].cap=0;
    e[tol].cost=-cost;
    e[tol].flow=0;
    e[tol].next=head[v];
    head[v]=tol++;
}

bool spfa(int s,int t){
    queue<int> q;
    for(int i=0;i<N;i++){
        dis[i]=inf;
        vis[i]=false;
        pre[i]=-1;
    }
    dis[s]=0;
    vis[s]=1;
    q.push(s);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=head[u];~i;i=e[i].next){
            int v=e[i].to;
            if(e[i].cap>e[i].flow&&dis[v]>dis[u]+e[i].cost){
                dis[v]=dis[u]+e[i].cost;
                pre[v]=i;
                if(!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    if(pre[t]==-1)return 0;
    return 1;
}

int minCostMaxflow(int s,int t,int &cost){
    int flow=0;
    cost=0;
    while(spfa(s,t)){
        int Min=inf;
        for(int i=pre[t];~i;i=pre[e[i^1].to]){
            if(Min>e[i].cap-e[i].flow)
                Min=e[i].cap-e[i].flow;
        }
        for(int i=pre[t];~i;i=pre[e[i^1].to]){
            e[i].flow+=Min;
            e[i^1].flow-=Min;
            cost+=e[i].cost*Min;
        }
        flow+=Min;
    }
    return flow;
}

char ch[20];

int32_t main(){
//    IN;
    IOS;
    int a,b,c;cin>>a>>b>>c;
    int n;cin>>n;
    int st=0,s1=1,s2=2,s3=3;
    int ed=n+20;
    init(ed+55);
    addedge(st,s1,a,0);
    addedge(st,s2,b,0);
    addedge(st,s3,c,0);
    int id=4;
    for(int i=1,x;i<=n;i++){
        cin>>x>>ch;
        if(ch[0]=='U'){
            addedge(s1,++id,1,x);
            addedge(s3,id,1,x);
        }else{
            addedge(s2,++id,1,x);
            addedge(s3,id,1,x);
        }
        addedge(id,ed,1,0);
    }
    int co;
    int flow=minCostMaxflow(st,ed,co);
    cout<<flow<<' '<<co<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define IN freopen("in.txt", "r", stdin);
#define OUT freopen("out.txt","w",stdout);
#define endl '\n'
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;

const int N = 3e5+10;

pii ar[N];
char ch[20];

int main(){
//    IN;
    IOS;
    int a,b,c;cin>>a>>b>>c;
    int n;cin>>n;
    multiset<pii> s;
    for(int i=1,x;i<=n;i++){
        cin>>x>>ch;
        if(ch[0]=='U')ar[i]={x,0};
        else ar[i]={x,1};
    }
    sort(ar+1,ar+1+n);
    ll cnt=0,ans=0;
    for(int i=1;i<=n;i++){
        if(!ar[i].se && a)a--,cnt++,ans+=ar[i].fi;
        else if(ar[i].se && b)b--,cnt++,ans+=ar[i].fi;
        else if(c)c--,cnt++,ans+=ar[i].fi;
    }
    cout<<cnt<<' '<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define IN freopen("in.txt", "r", stdin);
#define OUT freopen("out.txt","w",stdout);
#define endl '\n'
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;

char ch[20];

int main(){
//    IN;
    IOS;
    int a,b,c;cin>>a>>b>>c;
    int n;cin>>n;
    multiset<pii> s;
    for(int i=1,x;i<=n;i++){
        cin>>x>>ch;
        if(ch[0]=='U')s.insert({x,0});
        else s.insert({x,1});
    }
    ll cnt=0,ans=0;
    while(!s.empty()){
        pii tp=*s.begin();
        s.erase(tp);
        int t=tp.se;
        if(!t){
            if(a){
                a--;
                cnt++;
                ans+=tp.fi;
            }else if(c){
                c--;
                cnt++;
                ans+=tp.fi;
            }
            continue;
        }
        if(b){
            b--;
            cnt++;
            ans+=tp.fi;
        }else if(c){
            c--;
            cnt++;
            ans+=tp.fi;
        }
    }
    cout<<cnt<<' '<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define IN freopen("in.txt", "r", stdin);
#define OUT freopen("out.txt","w",stdout);
#define endl '\n'
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;

char ch[20];

int main(){
//    IN;
    IOS;
    int a,b,c;cin>>a>>b>>c;
    int n;cin>>n;
    set<pii> s;
    for(int i=1,x;i<=n;i++){
        cin>>x>>ch;
        if(ch[0]=='U')s.insert({x,0});
        else s.insert({x,1});
    }
    ll cnt=0,ans=0;
    while(!s.empty()){
        pii tp=*s.begin();
        s.erase(tp);
        int t=tp.se;
        if(!t){
            if(a){
                a--;
                cnt++;
                ans+=tp.fi;
            }else if(c){
                c--;
                cnt++;
                ans+=tp.fi;
            }
            continue;
        }
        if(b){
            b--;
            cnt++;
            ans+=tp.fi;
        }else if(c){
            c--;
            cnt++;
            ans+=tp.fi;
        }
    }
    cout<<cnt<<' '<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define IN freopen("in.txt", "r", stdin);
#define OUT freopen("out.txt","w",stdout);
#define endl '\n'
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;

const int MAXN = 2e5+10;
const int MAXM = 2e5+10;
struct Edge{
    int to,next,cap,flow,cost;
}e[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;
void init(int n){
    N=n;
    tol=0;
    memset(head,-1,sizeof head);
}
void addedge(int u,int v,int cap,int cost){
    e[tol].to=v;
    e[tol].cap=cap;
    e[tol].cost=cost;
    e[tol].flow=0;
    e[tol].next=head[u];
    head[u]=tol++;
    e[tol].to=u;
    e[tol].cap=0;
    e[tol].cost=-cost;
    e[tol].flow=0;
    e[tol].next=head[v];
    head[v]=tol++;
}

bool spfa(int s,int t){
    queue<int> q;
    for(int i=0;i<N;i++){
        dis[i]=inf;
        vis[i]=false;
        pre[i]=-1;
    }
    dis[s]=0;
    vis[s]=1;
    q.push(s);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=head[u];~i;i=e[i].next){
            int v=e[i].to;
            if(e[i].cap>e[i].flow&&dis[v]>dis[u]+e[i].cost){
                dis[v]=dis[u]+e[i].cost;
                pre[v]=i;
                if(!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    if(pre[t]==-1)return 0;
    return 1;
}

int minCostMaxflow(int s,int t,int &cost){
    int flow=0;
    cost=0;
    while(spfa(s,t)){
        int Min=inf;
        for(int i=pre[t];~i;i=pre[e[i^1].to]){
            if(Min>e[i].cap-e[i].flow)
                Min=e[i].cap-e[i].flow;
        }
        for(int i=pre[t];~i;i=pre[e[i^1].to]){
            e[i].flow+=Min;
            e[i^1].flow-=Min;
            cost+=e[i].cost*Min;
        }
        flow+=Min;
    }
    return flow;
}

char ch[20];

int main(){
//    IN;
    IOS;
    int a,b,c;cin>>a>>b>>c;
    int n;cin>>n;
    int st=0,s1=1,s2=2,s3=3;
    int ed=n+20;
    init(ed+55);
    addedge(st,s1,a,0);
    addedge(st,s2,b,0);
    addedge(st,s3,c,0);
    int id=4;
    for(int i=1,x;i<=n;i++){
        cin>>x>>ch;
        if(ch[0]=='U'){
            addedge(s1,++id,1,x);
            addedge(s3,id,1,x);
        }else{
            addedge(s2,++id,1,x);
            addedge(s3,id,1,x);
        }
        addedge(id,ed,1,0);
    }
    int co;
    int flow=minCostMaxflow(st,ed,co);
    cout<<flow<<' '<<co<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define IN freopen("in.txt", "r", stdin);
#define OUT freopen("out.txt","w",stdout);
#define endl '\n'
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define int long long
#define sz(x) (int)x.size()
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const ll inf = 0x3f3f3f3f3f3f3f3f;

const int MAXN = 2e5+10;
const int MAXM = 2e5+10;
struct Edge{
    int to,next,cap,flow,cost;
}e[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;
void init(int n){
    N=n;
    tol=0;
    memset(head,-1,sizeof head);
}
void addedge(int u,int v,int cap,int cost){
    e[tol].to=v;
    e[tol].cap=cap;
    e[tol].cost=cost;
    e[tol].flow=0;
    e[tol].next=head[u];
    head[u]=tol++;
    e[tol].to=u;
    e[tol].cap=0;
    e[tol].cost=-cost;
    e[tol].flow=0;
    e[tol].next=head[v];
    head[v]=tol++;
}

bool spfa(int s,int t){
    queue<int> q;
    for(int i=0;i<N;i++){
        dis[i]=inf;
        vis[i]=false;
        pre[i]=-1;
    }
    dis[s]=0;
    vis[s]=1;
    q.push(s);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=head[u];~i;i=e[i].next){
            int v=e[i].to;
            if(e[i].cap>e[i].flow&&dis[v]>dis[u]+e[i].cost){
                dis[v]=dis[u]+e[i].cost;
                pre[v]=i;
                if(!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    if(pre[t]==-1)return 0;
    return 1;
}

int minCostMaxflow(int s,int t,int &cost){
    int flow=0;
    cost=0;
    while(spfa(s,t)){
        int Min=inf;
        for(int i=pre[t];~i;i=pre[e[i^1].to]){
            if(Min>e[i].cap-e[i].flow)
                Min=e[i].cap-e[i].flow;
        }
        for(int i=pre[t];~i;i=pre[e[i^1].to]){
            e[i].flow+=Min;
            e[i^1].flow-=Min;
            cost+=e[i].cost*Min;
        }
        flow+=Min;
    }
    return flow;
}

char ch[20];

int32_t main(){
//    IN;
    IOS;
    int a,b,c;cin>>a>>b>>c;
    int n;cin>>n;
    int st=0,s1=1,s2=2,s3=3;
    int ed=n+20;
    init(ed+55);
    addedge(st,s1,a,0);
    addedge(st,s2,b,0);
    addedge(st,s3,c,0);
    int id=4;
    for(int i=1,x;i<=n;i++){
        cin>>x>>ch;
        if(ch[0]=='U'){
            addedge(s1,++id,1,x);
            addedge(s3,id,1,x);
        }else{
            addedge(s2,++id,1,x);
            addedge(s3,id,1,x);
        }
        addedge(id,ed,1,0);
    }
    int co;
    int flow=minCostMaxflow(st,ed,co);
    cout<<flow<<' '<<co<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pr;
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define f(i,k,n) for(ll i=k;i<n;i++)
#define fr(i,n,k) for(ll i=n-1;i>=k;i--)
#define pb push_back
#define mxi(v) (max_element((v).begin(),(v).end())-(v).begin())
#define mni(v) (min_element((v).begin(),(v).end())-(v).begin())
#define mxe(v) *max_element((v).begin(),(v).end())
#define mne(v) *min_element((v).begin(),(v).end())
#define srt(v) sort((v).begin(),(v).end())
#define rsrt(v) sort((v).begin(),(v).end(),greater<int>())
#define unq(v) (v).resize(distance((v).begin(),unique((v).begin(),(v).end())))
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define ve vector
const ll N=1e9+7,S=1e5+1;
/*
ll bex(ll a,ll b) {
    a%=N; ll ans=1;
    while(b>0) {if(b&1) {ans=(ans*a)%N;} a=(a*a)%N; b/=2;}
    return ans;
}*/
/*
ll prime[S]={0};
void seive() {
    prime[0]=prime[1]=1;
    for(ll i=2;i<S;i++) {
        if(!prime[i] && (ll)i*i<S) {
            prime[i]=i;
            for(ll j=i*i;j<S;j+=i) {
                if(!prime[j]) {prime[j]=i;}
            }
        }
    }
}*/
int main() {
    fast
    /*freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);*/
    ll t=1; //cin>>t;
    while(t--) {
        ll a,b,c; cin>>a>>b>>c;
        ll m,x; cin>>m;
        ve<pair<ll,string>>v;
        string s;
        f(i,0,m) {
            cin>>x>>s; v.pb({x,s});
        }
        srt(v);
        ll i=0,j=0,cnt=0; x=0;
        f(i,0,m) {
            if(v[i].second=="USB" && a) {x+=v[i].first; cnt++; a--;}
            else if(v[i].second=="PS/2" && b) {x+=v[i].first; cnt++; b--;}
            else if(c) {x+=v[i].first; cnt++; c--;}
        }
        cout<<cnt<<" "<<x<<endl;
    }
return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pr;
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define f(i,k,n) for(ll i=k;i<n;i++)
#define fr(i,n,k) for(ll i=n-1;i>=k;i--)
#define pb push_back
#define mxi(v) (max_element((v).begin(),(v).end())-(v).begin())
#define mni(v) (min_element((v).begin(),(v).end())-(v).begin())
#define mxe(v) *max_element((v).begin(),(v).end())
#define mne(v) *min_element((v).begin(),(v).end())
#define srt(v) sort((v).begin(),(v).end())
#define rsrt(v) sort((v).begin(),(v).end(),greater<int>())
#define unq(v) (v).resize(distance((v).begin(),unique((v).begin(),(v).end())))
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define ve vector
const ll N=1e9+7,S=1e5+1;
/*
ll bex(ll a,ll b) {
    a%=N; ll ans=1;
    while(b>0) {if(b&1) {ans=(ans*a)%N;} a=(a*a)%N; b/=2;}
    return ans;
}*/
/*
ll prime[S]={0};
void seive() {
    prime[0]=prime[1]=1;
    for(ll i=2;i<S;i++) {
        if(!prime[i] && (ll)i*i<S) {
            prime[i]=i;
            for(ll j=i*i;j<S;j+=i) {
                if(!prime[j]) {prime[j]=i;}
            }
        }
    }
}*/
int main() {
    fast
    /*freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);*/
    ll t=1; //cin>>t;
    while(t--) {
        ll a,b,c; cin>>a>>b>>c;
        ll m,x; cin>>m;
        ve<ll>u,p;
        string s;
        f(i,0,m) {
            cin>>x>>s;
            if(s[0]=='U') {u.pb(x);}
            else if(s[0]=='P') {p.pb(x);}
        }
        srt(u); srt(p);
        ll i=0,j=0,cnt=0; x=0;
        while(i<a && i<u.size()) {x+=u[i]; cnt++; i++;}
        while(j<b && j<p.size()) {x+=p[j]; cnt++; j++;}
        if(i<u.size() && j<p.size() && c) {
            if(u[i]<p[j]) {x+=u[i]; cnt++; i++; c--;}
            else {x+=p[j]; j++; cnt++; c--;}
        }
        while(i<u.size() && c) {x+=u[i]; cnt++; i++; c--;}
        while(j<p.size() && c) {x+=p[j]; cnt++; j++; c--;}
        cout<<cnt<<" "<<x<<endl;
    }
return 0;
}


#include<iostream>
#include<vector>
#include<math.h>
#include<string>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<list>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<iterator>
#include<numeric>
#include<iomanip>
#include<limits>
using namespace std;

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define int long long int
#define double long double
template< typename T>
void print1d(vector<T> &a){
    for(int i=0;i<a.size();i++){
        cout << a[i] << " ";
    }
    cout << endl;
}
vector<int> divisor(int n){
    vector<int> a;
    for (int i = 1; i*i <=n; i++)
    {
        if(n%i==0){
            if(n/i==i)a.push_back(i);
            else{
                a.push_back(i);
                a.push_back(n/i);
            }
        }
    }
    return a;
}
int K = 1E9;
int32_t main(){
    FAST;
    int a,b,c;
    cin >> a >> b >> c;
    int m = 0;
    cin >> m;
    set<int> p,q;
    for (int i = 0; i < m; i++)
    {
        int x;
        string s;
        cin >> x >> s;
        if(s[0]=='U')p.insert(x);
        else q.insert(x);
    }
    int x;
    int cnt=0,sum=0;
    int y = min((int)p.size(),a);
    for (int i = 0; i < y; i++)
    {
        x = *(p.begin());
        p.erase(p.begin());
        sum+=x;
        cnt++;
    }
    // cout << cnt << " " << q.size() << " " << b << endl;
    y = min((int)q.size(),b);
    for (int i = 0; i <y; i++)
    {
        x = *(q.begin());
        q.erase(q.begin());
        sum+=x;
        cnt++;
        // cout << "hi ";
    }
    // cout << endl;
    // cout << cnt << endl;
    set<int> :: iterator pt,qt;
    pt = p.begin();
    qt = q.begin();
    while(c!=0&&(pt!=p.end()||qt!=q.end())){
        if(pt==p.end()&&qt==q.end())break;
        if(c<=0)break;
        if(pt==p.end()){
            sum+=*qt;
            cnt++;
            qt++;
            c--;
        }else if(qt==q.end()){
            sum+=*pt;
            cnt++;
            pt++;
            c--;
        }
        else {
            if(*pt < *qt){
                sum+=*pt;
                cnt++;
                pt++;
                c--;
            }else{
                sum+=*qt;
                cnt++;
                qt++;
                c--;
            }
        }
        // cout << cnt << endl;
    }
    cout << cnt << " " << sum << endl;
    return 0;
}


#include<iostream>
#include<vector>
#include<math.h>
#include<string>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<list>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<iterator>
#include<numeric>
#include<iomanip>
#include<limits>
using namespace std;

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define int long long int
#define double long double
template< typename T>
void print1d(vector<T> &a){
    for(int i=0;i<a.size();i++){
        cout << a[i] << " ";
    }
    cout << endl;
}
vector<int> divisor(int n){
    vector<int> a;
    for (int i = 1; i*i <=n; i++)
    {
        if(n%i==0){
            if(n/i==i)a.push_back(i);
            else{
                a.push_back(i);
                a.push_back(n/i);
            }
        }
    }
    return a;
}
int K = 1E9;
int32_t main(){
    FAST;
    int a,b,c;
    cin >> a >> b >> c;
    int m = 0;
    cin >> m;
    set<int> p,q;
    for (int i = 0; i < m; i++)
    {
        int x;
        string s;
        cin >> x >> s;
        if(s[0]='U')p.insert(x);
        else q.insert(x);
    }
    int x;
    int cnt=0,sum=0;
    for (int i = 0; i < min((int)p.size(),a); i++)
    {
        x = *(p.begin());
        p.erase(p.begin());
        sum+=x;
        cnt++;
    }
    for (int i = 0; i <min(b,(int)q.size()); i++)
    {
        x = *(p.begin());
        p.erase(p.begin());
        sum+=x;
        cnt++;
    }
    set<int> :: iterator pt,qt;
    pt = p.begin();
    qt = q.begin();
    while(c!=0&&(pt!=p.end()||qt!=q.end())){
        if(pt==p.end()&&qt==q.end())break;
        if(pt==p.end()){
            sum+=*qt;
            cnt++;
            qt++;
            c--;
        }else if(qt==q.end()){
            sum+=*pt;
            cnt++;
            pt++;
            c--;
        }
        else {
            if(*pt<=*qt){
                sum+=*pt;
                cnt++;
                pt++;
                c--;
            }else{
                sum+=*qt;
                cnt++;
                qt++;
                c--;
            }
        }
    }
    cout << cnt << " " << sum << endl;
    return 0;
}


#include<iostream>
#include<vector>
#include<math.h>
#include<string>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<list>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<iterator>
#include<numeric>
#include<iomanip>
#include<limits>
using namespace std;

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define int long long int
#define double long double
template< typename T>
void print1d(vector<T> &a){
    for(int i=0;i<a.size();i++){
        cout << a[i] << " ";
    }
    cout << endl;
}
vector<int> divisor(int n){
    vector<int> a;
    for (int i = 1; i*i <=n; i++)
    {
        if(n%i==0){
            if(n/i==i)a.push_back(i);
            else{
                a.push_back(i);
                a.push_back(n/i);
            }
        }
    }
    return a;
}
int K = 1E9;
int32_t main(){
    FAST;
    int a,b,c;
    cin >> a >> b >> c;
    int m = 0;
    cin >> m;
    set<int> p,q;
    for (int i = 0; i < m; i++)
    {
        int x;
        string s;
        cin >> x >> s;
        if(s[0]=='U')p.insert(x);
        else q.insert(x);
    }
    int x;
    int cnt=0,sum=0;
    int y = min((int)p.size(),a);
    for (int i = 0; i < y; i++)
    {
        x = *(p.begin());
        p.erase(p.begin());
        sum+=x;
        cnt++;
    }
    // cout << cnt << " " << q.size() << " " << b << endl;
    y = min((int)q.size(),b);
    for (int i = 0; i <y; i++)
    {
        x = *(q.begin());
        q.erase(q.begin());
        sum+=x;
        cnt++;
        // cout << "hi ";
    }
    // cout << endl;
    // cout << cnt << endl;
    set<int> :: iterator pt,qt;
    pt = p.begin();
    qt = q.begin();
    while(c!=0&&(pt!=p.end()||qt!=q.end())){
        if(pt==p.end()&&qt==q.end())break;
        if(pt==p.end()){
            sum+=*qt;
            cnt++;
            qt++;
            c--;
        }else if(qt==q.end()){
            sum+=*pt;
            cnt++;
            pt++;
            c--;
        }
        else {
            if(*pt < *qt){
                sum+=*pt;
                cnt++;
                pt++;
                c--;
            }else{
                sum+=*qt;
                cnt++;
                qt++;
                c--;
            }
        }
        // cout << cnt << endl;
    }
    cout << cnt << " " << sum << endl;
    return 0;
}


#include<iostream>
#include<vector>
#include<math.h>
#include<string>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<list>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<iterator>
#include<numeric>
#include<iomanip>
#include<limits>
using namespace std;

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define int long long int
#define double long double
template< typename T>
void print1d(vector<T> &a){
    for(int i=0;i<a.size();i++){
        cout << a[i] << " ";
    }
    cout << endl;
}
vector<int> divisor(int n){
    vector<int> a;
    for (int i = 1; i*i <=n; i++)
    {
        if(n%i==0){
            if(n/i==i)a.push_back(i);
            else{
                a.push_back(i);
                a.push_back(n/i);
            }
        }
    }
    return a;
}
int K = 1E9;
int32_t main(){
    FAST;
    int a,b,c;
    cin >> a >> b >> c;
    int m = 0;
    cin >> m;
    set<int> p,q;
    for (int i = 0; i < m; i++)
    {
        int x;
        string s;
        cin >> x >> s;
        if(s[0]=='U')p.insert(x);
        else q.insert(x);
    }
    int x;
    int cnt=0,sum=0;
    int y = min((int)p.size(),a);
    for (int i = 0; i < y; i++)
    {
        x = *(p.begin());
        p.erase(p.begin());
        sum+=x;
        cnt++;
    }
    // cout << cnt << " " << q.size() << " " << b << endl;
    y = min((int)q.size(),b);
    for (int i = 0; i <y; i++)
    {
        x = *(q.begin());
        q.erase(q.begin());
        sum+=x;
        cnt++;
        // cout << "hi ";
    }
    // cout << endl;
    // cout << cnt << endl;
    set<int> :: iterator pt,qt;
    pt = p.begin();
    qt = q.begin();
    while(c!=0&&(pt!=p.end()||qt!=q.end())){
        if(pt==p.end()&&qt==q.end())break;
        if(pt==p.end()){
            sum+=*qt;
            cnt++;
            qt++;
            c--;
        }else if(qt==q.end()){
            sum+=*pt;
            cnt++;
            pt++;
            c--;
        }
        else {
            if(*pt<=*qt){
                sum+=*pt;
                cnt++;
                pt++;
                c--;
            }else{
                sum+=*qt;
                cnt++;
                qt++;
                c--;
            }
        }
        // cout << cnt << endl;
    }
    cout << cnt << " " << sum << endl;
    return 0;
}


#include<iostream>
#include<vector>
#include<math.h>
#include<string>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<list>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<iterator>
#include<numeric>
#include<iomanip>
#include<limits>
using namespace std;

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define int long long int
#define double long double
template< typename T>
void print1d(vector<T> &a){
    for(int i=0;i<a.size();i++){
        cout << a[i] << " ";
    }
    cout << endl;
}
vector<int> divisor(int n){
    vector<int> a;
    for (int i = 1; i*i <=n; i++)
    {
        if(n%i==0){
            if(n/i==i)a.push_back(i);
            else{
                a.push_back(i);
                a.push_back(n/i);
            }
        }
    }
    return a;
}
int K = 1E9;
int32_t main(){
    FAST;
    int a,b,c;
    cin >> a >> b >> c;
    int m = 0;
    cin >> m;
    // set<int> p,q;
    int W = numeric_limits<int> :: max();
    vector<int> p(m,W),q(m,W);
    int ps=0,qs=0;
    for (int i = 0; i < m; i++)
    {
        int x;
        string s;
        cin >> x >> s;
        // if(s[0]=='U')p.insert(x);
        // else q.insert(x);
        if(s[0]=='U'){
            p[ps]=x;
            ps++;
        }else{
            q[qs]=x;
            qs++;
        }
    }
    sort(p.begin(),p.end());
    sort(q.begin(),q.end());
    int pt=0,qt=0;
    int x;
    int cnt=0,sum=0;
    int y = min((int)ps,a);
    for (int i = 0; i < y; i++)
    {
        // x = *(p.begin());
        // p.erase(p.begin());
        x = p[pt];
        pt++;
        sum+=x;
        cnt++;
    }
    // cout << cnt << " " << q.size() << " " << b << endl;
    y = min((int)qs,b);
    for (int i = 0; i <y; i++)
    {
        // x = *(q.begin());
        // q.erase(q.begin());
        x = q[qt];
        qt++;
        sum+=x;
        cnt++;
        // cout << "hi ";
    }
    // cout << endl;
    // cout << cnt << endl;
    // set<int> :: iterator pt,qt;
    // pt = p.begin();
    // qt = q.begin();
    while(c!=0){
        if(pt>=ps&&qt>=qs)break;
        if(c<=0)break;
        if(pt>=ps){
            sum+=q[qt];
            cnt++;
            qt++;
            c--;
        }else if(qt>=qs){
            sum+=p[pt];
            cnt++;
            pt++;
            c--;
        }
        else {
            if(p[pt]<q[qt]){
                sum+=p[pt];
                cnt++;
                pt++;
                c--;
            }else{
                sum+=q[qt];
                cnt++;
                qt++;
                c--;
            }
        }
        // cout << cnt << endl;
    }
    cout << cnt << " " << sum << endl;
    return 0;
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
 long long a,b,c,i=0,j=0,k,l,sum1=0,sum2=0,sum3=0,m,n=0,M,x;
long long USB[30000],PS[30000],BOTH[30000];
int main()
{
   long long int count=0;

    cin>>a>>b>>c>>M;
    while(M--)
    {

        char arr[5];
        cin>>x;
        cin>>arr;
        if(strcmp(arr,"USB")==0)
        {
            USB[i]=x;
            i++;
        }
        else if(strcmp(arr,"PS/2")==0)
        {
            PS[j]=x;
            j++;
        }

    }

    if(i!=0 && i<=a)
    {
        sort(USB,USB+i);
        for(k=0;k<i;k++)
        {
            sum1+=USB[k];
            count++;
        }

    }
    if(i>a)
    {
        sort(USB,USB+i);
        for(k=0;k<a;k++)
        {
            sum1+=USB[k];
            count++;

        }

          for(m=a;m<i;m++)
          {
              BOTH[n]=USB[m];

              n++;
          }

    }
    if(j!=0 && j<=b)
    {
        sort(PS,PS+j);
        for(l=0;l<j;l++)
        {
            sum2+=PS[l];
            count++;
        }

    }
    if(j>b)
    {
        sort(PS,PS+j);
        for(l=0; l<b; l++)
        {
            sum2+=PS[l];
            count++;

        }

         for(m=b;m<j;m++)
          {
              BOTH[n]=PS[m];
              n++;
          }

    }
    if(n!=0 && n<=c)
    {
    sort(BOTH,BOTH+n);
    for(l=0;l<n;l++)
    {

        sum3+=BOTH[l];
        count++;
    }
    }
    if(n>c)
    {
        sort(BOTH,BOTH+n);
        for(l=0;l<c;l++)
        {
            sum3+=BOTH[l];
            count++;
        }
    }

    cout<<count<<" "<<sum1+sum2+sum3<<endl;

}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
 long long a,b,c,i=0,j=0,k,l,sum1=0,sum2=0,sum3=0,m,n=0,M,x;
long long USB[10000],PS[10000],BOTH[10000];
int main()
{
   long long int count=0;

    cin>>a>>b>>c>>M;
    while(M--)
    {

        char arr[5];
        cin>>x;
        cin>>arr;
        if(strcmp(arr,"USB")==0)
        {
            USB[i]=x;
            i++;
        }
        else if(strcmp(arr,"PS/2")==0)
        {
            PS[j]=x;
            j++;
        }

    }

    if(i!=0 && i<=a)
    {
        sort(USB,USB+i);
        for(k=0;k<i;k++)
        {
            sum1+=USB[k];
            count++;
        }

    }
    if(i>a)
    {
        sort(USB,USB+i);
        for(k=0;k<a;k++)
        {
            sum1+=USB[k];
            count++;

        }

          for(m=a;m<i;m++)
          {
              BOTH[n]=USB[m];

              n++;
          }

    }
    if(j!=0 && j<=b)
    {
        sort(PS,PS+j);
        for(l=0;l<j;l++)
        {
            sum2+=PS[l];
            count++;
        }

    }
    if(j>b)
    {
        sort(PS,PS+j);
        for(l=0; l<b; l++)
        {
            sum2+=PS[l];
            count++;

        }

         for(m=b;m<j;m++)
          {
              BOTH[n]=PS[m];
              n++;
          }

    }
    if(n!=0 && n<=c)
    {
    sort(BOTH,BOTH+n);
    for(l=0;l<n;l++)
    {

        sum3+=BOTH[l];
        count++;
    }
    }
    if(n>c)
    {
        sort(BOTH,BOTH+n);
        for(l=0;l<c;l++)
        {
            sum3+=BOTH[l];
            count++;
        }
    }

    cout<<count<<" "<<sum1+sum2+sum3<<endl;

}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
unsigned long long long int USB[50000],PS[50000],BOTH[50000];
int main()
{
    unsigned long long long int a,b,c,i=0,j=0,k,l,sum1=0,sum2=0,sum3=0,m,n=0,count=0,M;

    cin>>a>>b>>c>>M;
    while(M--)
    {
        unsigned long long long int x;
        char arr[5];
        cin>>x;
        cin>>arr;
        if(strcmp(arr,"USB")==0)
        {
            USB[i]=x;
            i++;
        }
        else if(strcmp(arr,"PS/2")==0)
        {
            PS[j]=x;
            j++;
        }

    }

    if(i!=0 && i<=a)
    {
        sort(USB,USB+i);
        for(k=0;k<i;k++)
        {
            sum1+=USB[k];
            count++;
        }

    }
    if(i>a)
    {
        sort(USB,USB+i);
        for(k=0;k<a;k++)
        {
            sum1+=USB[k];
            count++;

        }

          for(m=a;m<i;m++)
          {
              BOTH[n]=USB[m];

              n++;
          }

    }
    if(j!=0 && j<=b)
    {
        sort(PS,PS+j);
        for(l=0;l<j;l++)
        {
            sum2+=PS[l];
            count++;
        }

    }
    if(j>b)
    {
        sort(PS,PS+j);
        for(l=0; l<b; l++)
        {
            sum2+=PS[l];
            count++;

        }

         for(m=b;m<j;m++)
          {
              BOTH[n]=PS[m];
              n++;
          }

    }
    if(n!=0 && n<=c)
    {
    sort(BOTH,BOTH+n);
    for(l=0;l<n;l++)
    {

        sum3+=BOTH[l];
        count++;
    }
    }
    if(n>c)
    {
        sort(BOTH,BOTH+n);
        for(l=0;l<c;l++)
        {
            sum3+=BOTH[l];
            count++;
        }
    }

    cout<<count<<" "<<sum1+sum2+sum3<<endl;

}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
 long long a,b,c,i=0,j=0,k,l,sum1=0,sum2=0,sum3=0,m,n=0,M,x;
long long USB[300000];
long long PS[300000];
long long BOTH[300000];
int main()
{
   long long int count=0;

    cin>>a>>b>>c>>M;
    while(M--)
    {

        char arr[5];
        cin>>x;
        cin>>arr;
        if(strcmp(arr,"USB")==0)
        {
            USB[i]=x;
            i++;
        }
        else if(strcmp(arr,"PS/2")==0)
        {
            PS[j]=x;
            j++;
        }

    }

    if(i!=0 && i<=a)
    {
        sort(USB,USB+i);
        for(k=0;k<i;k++)
        {
            sum1+=USB[k];
            count++;
        }

    }
    if(i>a)
    {
        sort(USB,USB+i);
        for(k=0;k<a;k++)
        {
            sum1+=USB[k];
            count++;

        }

          for(m=a;m<i;m++)
          {
              BOTH[n]=USB[m];

              n++;
          }

    }
    if(j!=0 && j<=b)
    {
        sort(PS,PS+j);
        for(l=0;l<j;l++)
        {
            sum2+=PS[l];
            count++;
        }

    }
    if(j>b)
    {
        sort(PS,PS+j);
        for(l=0; l<b; l++)
        {
            sum2+=PS[l];
            count++;

        }

         for(m=b;m<j;m++)
          {
              BOTH[n]=PS[m];
              n++;
          }

    }
    if(n!=0 && n<=c)
    {
    sort(BOTH,BOTH+n);
    for(l=0;l<n;l++)
    {

        sum3+=BOTH[l];
        count++;
    }
    }
    if(n>c)
    {
        sort(BOTH,BOTH+n);
        for(l=0;l<c;l++)
        {
            sum3+=BOTH[l];
            count++;
        }
    }

    cout<<count<<" "<<sum1+sum2+sum3<<endl;

}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
int main()
{
    unsigned long long int a,b,c,i=0,j=0,k,l,sum1=0,sum2=0,sum3=0,m,n=0,count=0;
    unsigned long long int USB[3000],PS[3000],BOTH[3000];
    cin>>a>>b>>c>>m;
    while(m--)
    {
        unsigned long long int x;
        char arr[5];
        cin>>x;
        cin>>arr;
        if(strcmp(arr,"USB")==0)
        {
            USB[i]=x;
            i++;
        }
        else if(strcmp(arr,"PS/2")==0)
        {
            PS[j]=x;
            j++;
        }

    }

    if(i<=a)
    {
        sort(USB,USB+i);
        for(k=0; k<i; k++)
        {
            sum1+=USB[k];
            count++;
        }

    }
    if(i>a)
    {
        sort(USB,USB+i);
        for(k=0; k<a; k++)
        {
            sum1+=USB[k];
            count++;

        }

          for(m=a;m<i;m++)
          {
              BOTH[n]=USB[m];

              n++;
          }

    }
    if(j<=b)
    {
        sort(PS,PS+j);
        for(l=0; l<j; l++)
        {
            sum2+=PS[l];
            count++;
        }

    }
    if(j>b)
    {
        sort(PS,PS+j);
        for(l=0; l<b; l++)
        {
            sum2+=PS[l];
            count++;

        }

         for(m=b;m<j;m++)
          {
              BOTH[n]=PS[m];
              n++;
          }

    }
    sort(BOTH,BOTH+n);
    for(l=0;l<c;l++)
    {

        sum3+=BOTH[l];
        count++;
    }

    cout<<count<<" "<<sum1+sum2+sum3<<endl;

}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
 unsigned long long int a,b,c,i=0,j=0,k,l,sum1=0,sum2=0,sum3=0,m,n=0,M;
 unsigned long long int USB[30000],PS[30000],BOTH[30000];
int main()
{
   long long int count=0;

    cin>>a>>b>>c>>M;
    while(M--)
    {
        unsigned long long int x;
        char arr[5];
        cin>>x;
        cin>>arr;
        if(strcmp(arr,"USB")==0)
        {
            USB[i]=x;
            i++;
        }
        else if(strcmp(arr,"PS/2")==0)
        {
            PS[j]=x;
            j++;
        }

    }

    if(i!=0 && i<=a)
    {
        sort(USB,USB+i);
        for(k=0;k<i;k++)
        {
            sum1+=USB[k];
            count++;
        }

    }
    if(i>a)
    {
        sort(USB,USB+i);
        for(k=0;k<a;k++)
        {
            sum1+=USB[k];
            count++;

        }

          for(m=a;m<i;m++)
          {
              BOTH[n]=USB[m];

              n++;
          }

    }
    if(j!=0 && j<=b)
    {
        sort(PS,PS+j);
        for(l=0;l<j;l++)
        {
            sum2+=PS[l];
            count++;
        }

    }
    if(j>b)
    {
        sort(PS,PS+j);
        for(l=0; l<b; l++)
        {
            sum2+=PS[l];
            count++;

        }

         for(m=b;m<j;m++)
          {
              BOTH[n]=PS[m];
              n++;
          }

    }
    if(n!=0 && n<=c)
    {
    sort(BOTH,BOTH+n);
    for(l=0;l<n;l++)
    {

        sum3+=BOTH[l];
        count++;
    }
    }
    if(n>c)
    {
        sort(BOTH,BOTH+n);
        for(l=0;l<c;l++)
        {
            sum3+=BOTH[l];
            count++;
        }
    }

    cout<<count<<" "<<sum1+sum2+sum3<<endl;

}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
int main()
{
    unsigned long long int a,b,c,i=0,j=0,k,l,sum1=0,sum2=0,sum3=0,m,n=0,count=0;
    unsigned long long int USB[3000],PS[3000],BOTH[3000];
    cin>>a>>b>>c>>m;
    while(m--)
    {
        unsigned long long int x;
        char arr[5];
        cin>>x;
        cin>>arr;
        if(strcmp(arr,"USB")==0)
        {
            USB[i]=x;
            i++;
        }
        else if(strcmp(arr,"PS/2")==0)
        {
            PS[j]=x;
            j++;
        }

    }

    if(i!=0 && i<=a)
    {
        sort(USB,USB+i);
        for(k=0; k<i; k++)
        {
            sum1+=USB[k];
            count++;
        }

    }
    if(i>a)
    {
        sort(USB,USB+i);
        for(k=0; k<a; k++)
        {
            sum1+=USB[k];
            count++;

        }

          for(m=a;m<i;m++)
          {
              BOTH[n]=USB[m];

              n++;
          }

    }
    if(j!=0 && j<=b)
    {
        sort(PS,PS+j);
        for(l=0; l<j; l++)
        {
            sum2+=PS[l];
            count++;
        }

    }
    if(j>b)
    {
        sort(PS,PS+j);
        for(l=0; l<b; l++)
        {
            sum2+=PS[l];
            count++;

        }

         for(m=b;m<j;m++)
          {
              BOTH[n]=PS[m];
              n++;
          }

    }
    if(n!=0)
    {
    sort(BOTH,BOTH+n);
    for(l=0;l<c;l++)
    {

        sum3+=BOTH[l];
        count++;
    }
    }

    cout<<count<<" "<<sum1+sum2+sum3<<endl;

}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
 long long int a,b,c,i=0,j=0,k,l,sum1=0,sum2=0,sum3=0,m,n=0,M,x;
long long USB[30000],PS[30000],BOTH[30000];
int main()
{
   long long int count=0;

    cin>>a>>b>>c>>M;
    while(M--)
    {

        char arr[5];
        cin>>x;
        cin>>arr;
        if(strcmp(arr,"USB")==0)
        {
            USB[i]=x;
            i++;
        }
        else if(strcmp(arr,"PS/2")==0)
        {
            PS[j]=x;
            j++;
        }

    }

    if(i!=0 && i<=a)
    {
        sort(USB,USB+i);
        for(k=0;k<i;k++)
        {
            sum1+=USB[k];
            count++;
        }

    }
    if(i>a)
    {
        sort(USB,USB+i);
        for(k=0;k<a;k++)
        {
            sum1+=USB[k];
            count++;

        }

          for(m=a;m<i;m++)
          {
              BOTH[n]=USB[m];

              n++;
          }

    }
    if(j!=0 && j<=b)
    {
        sort(PS,PS+j);
        for(l=0;l<j;l++)
        {
            sum2+=PS[l];
            count++;
        }

    }
    if(j>b)
    {
        sort(PS,PS+j);
        for(l=0; l<b; l++)
        {
            sum2+=PS[l];
            count++;

        }

         for(m=b;m<j;m++)
          {
              BOTH[n]=PS[m];
              n++;
          }

    }
    if(n!=0 && n<=c)
    {
    sort(BOTH,BOTH+n);
    for(l=0;l<n;l++)
    {

        sum3+=BOTH[l];
        count++;
    }
    }
    if(n>c)
    {
        sort(BOTH,BOTH+n);
        for(l=0;l<c;l++)
        {
            sum3+=BOTH[l];
            count++;
        }
    }

    cout<<count<<" "<<sum1+sum2+sum3<<endl;

}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
int main()
{
    unsigned long long int a,b,c,i=0,j=0,k,l,sum1=0,sum2=0,sum3=0,m,n=0,count=0;
    unsigned long long int USB[3000],PS[3000],BOTH[3000];
    cin>>a>>b>>c>>m;
    while(m--)
    {
        unsigned long long int x;
        char arr[5];
        cin>>x;
        cin>>arr;
        if(strcmp(arr,"USB")==0)
        {
            USB[i]=x;
            i++;
        }
        else if(strcmp(arr,"PS/2")==0)
        {
            PS[j]=x;
            j++;
        }

    }

    if(i!=0 && i<=a)
    {
        sort(USB,USB+i);
        for(k=0; k<i; k++)
        {
            sum1+=USB[k];
            count++;
        }

    }
    if(i>a)
    {
        sort(USB,USB+i);
        for(k=0; k<a; k++)
        {
            sum1+=USB[k];
            count++;

        }

          for(m=a;m<i;m++)
          {
              BOTH[n]=USB[m];

              n++;
          }

    }
    if(j!=0 && j<=b)
    {
        sort(PS,PS+j);
        for(l=0; l<j; l++)
        {
            sum2+=PS[l];
            count++;
        }

    }
    if(j>b)
    {
        sort(PS,PS+j);
        for(l=0; l<b; l++)
        {
            sum2+=PS[l];
            count++;

        }

         for(m=b;m<j;m++)
          {
              BOTH[n]=PS[m];
              n++;
          }

    }
    sort(BOTH,BOTH+n);
    for(l=0;l<c;l++)
    {

        sum3+=BOTH[l];
        count++;
    }
    sum1=sum1+sum2+sum3;
    cout<<count<<" "<<sum1<<endl;

}