Problem: Kostya is a progamer specializing in the discipline of Dota 2. Valve Corporation, the developer of this game, has recently released a new patch which turned the balance of the game upside down. Kostya, as the captain of the team, realizes that the greatest responsibility lies on him, so he wants to resort to the analysis of innovations patch from the mathematical point of view to choose the best heroes for his team in every game.

A Dota 2 match involves two teams, each of them must choose some heroes that the players of the team are going to play for, and it is forbidden to choose the same hero several times, even in different teams. In large electronic sports competitions where Kostya's team is going to participate, the matches are held in the Captains Mode. In this mode the captains select the heroes by making one of two possible actions in a certain, predetermined order: pick or ban.

  * To pick a hero for the team. After the captain picks, the picked hero goes to his team (later one of a team members will play it) and can no longer be selected by any of the teams. 
  * To ban a hero. After the ban the hero is not sent to any of the teams, but it still can no longer be selected by any of the teams. 



The team captain may miss a pick or a ban. If he misses a pick, a random hero is added to his team from those that were available at that moment, and if he misses a ban, no hero is banned, as if there was no ban.

Kostya has already identified the strength of all the heroes based on the new patch fixes. Of course, Kostya knows the order of picks and bans. The strength of a team is the sum of the strengths of the team's heroes and both teams that participate in the match seek to maximize the difference in strengths in their favor. Help Kostya determine what team, the first one or the second one, has advantage in the match, and how large the advantage is.

Input

The first line contains a single integer n (2 ≤ n ≤ 100) — the number of heroes in Dota 2.

The second line contains n integers s1, s2, ..., sn (1 ≤ si ≤ 106) — the strengths of all the heroes.

The third line contains a single integer m (2 ≤ m ≤ min(n, 20)) — the number of actions the captains of the team must perform.

Next m lines look like "action team", where action is the needed action: a pick (represented as a "p") or a ban (represented as a "b"), and team is the number of the team that needs to perform the action (number 1 or 2).

It is guaranteed that each team makes at least one pick. Besides, each team has the same number of picks and the same number of bans.

Output

Print a single integer — the difference between the strength of the first team and the strength of the second team if the captains of both teams will act optimally well.

Examples

Input

2
2 1
2
p 1
p 2


Output

1


Input

6
6 4 5 4 5 5
4
b 2
p 1
b 1
p 2


Output

0


Input

4
1 2 3 4
4
p 2
b 2
p 1
b 1


Output

-2

Solution (in JAVA):

Solution:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

public class E {

    static int n,m,MAXN=100, MAXM=20;
    
    static Integer[] strength=new Integer[MAXN];
    static String[] moves=new String[MAXN];
    
    static int compute() {
        Arrays.sort(strength, 0, n, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }});
        
        int M=1<<m;
        int[][] f=new int[m][M];
        for (int i=0; i<m; i++) {
            Arrays.fill(f[i], Integer.MAX_VALUE);
        }
        
        String move=moves[m-1];
        for (int i=0; i<M; i++) {
            int lo=Integer.lowestOneBit(i);
            int idx=0;
            while((lo>>1)>0) {
                idx++;
                lo=lo>>1;
            }
            
            if(move.equals("p 1")) {
                f[m-1][i]=strength[idx];
            } else if(move.equals("p 2")) {
                f[m-1][i]=-strength[idx];
            } else  {
                f[m-1][i]=0;
            }
        }
        
        for (int i=m-2; i>=0; i--) {
            for (int j=0; j<M; j++) {
                int l=j;
                while (l>0) {
                    int lo=Integer.lowestOneBit(l);
                    int k=j-lo;
                    
                    
                    l-=lo;
                    int idx=0;
                    while((lo>>1)>0) {
                        idx++;
                        lo=lo>>1;
                    }
                    
                    int pi=i+1;
                    move=moves[i];
                    if(move.equals("p 1")) {
                        if(f[i][j]==Integer.MAX_VALUE) {
                            f[i][j]=f[pi][k]+strength[idx];
                        } else {
                            f[i][j]=Math.max(f[i][j], f[pi][k]+strength[idx]);
                        }
                    } else if(move.equals("p 2")) {
                        f[i][j]=Math.min(f[i][j], f[pi][k]-strength[idx]);
                    } else if(move.equals("b 1")) {
                        if(f[i][j]==Integer.MAX_VALUE) { 
                            f[i][j]=f[pi][k];
                        } else {
                            f[i][j]=Math.max(f[i][j], f[pi][k]);
                        }
                    } else if(move.equals("b 2")) {
                        f[i][j]=Math.min(f[i][j], f[pi][k]);
                    }
                }
            }
        }
        
        return f[0][M-1];
    }
    
    
    
    
    
    
    static int compute2(Map<Long, Integer> f, int state, int move) {
        long key=((long)move<<32)+(long)state;
        Integer ret=f.get(key);
        if(ret==null) {
            String smove=moves[move];
            int r=(smove.endsWith("1")? Integer.MIN_VALUE: Integer.MAX_VALUE);
            
            int s=state;
            while (s>0) {
                int lo=Integer.lowestOneBit(s);
                int next_state=state-lo;
                
                
                s-=lo;
                int idx=0;
                while((lo>>1)>0) {
                    idx++;
                    lo=lo>>1;
                }
                
                int nextr=move+1<m? compute2(f, next_state, move+1): 0;
                
                if(smove.equals("p 1")) {
                    r=Math.max(r, nextr+strength[idx]);
                    break;
                } else if(smove.equals("p 2")) {
                    r=Math.min(r, nextr-strength[idx]);
                    break;
                } else if(smove.equals("b 1")) {
                    r=Math.max(r, nextr);
                } else if(smove.equals("b 2")) {
                    r=Math.min(r, nextr);
                }
            }
            ret=r;
            f.put(key, ret);
        }
        
        return ret;
    }
    
    static int compute2() {
        Arrays.sort(strength, 0, n, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }});
        
        return compute2(new HashMap<Long, Integer>(), (1<<m)-1, 0);
    }
    
    static StringBuilder sb=new StringBuilder();
    static void parse(String l, Integer[] here) {
        sb.setLength(0);
        int len=l.length(), j=0;
        for (int i=0; i<len; i++) {
            char c=l.charAt(i);
            if(c==' ') {
                here[j++]=Integer.parseInt(sb.toString());
                sb.setLength(0);
            } else {
                sb.append(c);
            }
        }
        if(sb.length()>0) {
            here[j++]=Integer.parseInt(sb.toString());
        }
    }
    
    static void run_stream(InputStream ins) throws IOException {
        BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
        n=Integer.parseInt(r.readLine());
        parse(r.readLine(), strength);
        m=Integer.parseInt(r.readLine());
        for (int i=0; i<m; i++) {
            moves[i]=r.readLine();
        }
        
        //long t0=System.currentTimeMillis();
        System.out.println(compute2());
        //System.out.println(System.currentTimeMillis()-t0);
    }
    
    public static void main(String[] args) throws IOException {
        run_stream(System.in);
    }

}