Problem: One day, Hongcow goes to the store and sees a brand new deck of n special cards. Each individual card is either red or blue. He decides he wants to buy them immediately. To do this, he needs to play a game with the owner of the store.

This game takes some number of turns to complete. On a turn, Hongcow may do one of two things: 

  * Collect tokens. Hongcow collects 1 red token and 1 blue token by choosing this option (thus, 2 tokens in total per one operation). 
  * Buy a card. Hongcow chooses some card and spends tokens to purchase it as specified below. 



The i-th card requires ri red resources and bi blue resources. Suppose Hongcow currently has A red cards and B blue cards. Then, the i-th card will require Hongcow to spend max(ri - A, 0) red tokens, and max(bi - B, 0) blue tokens. Note, only tokens disappear, but the cards stay with Hongcow forever. Each card can be bought only once.

Given a description of the cards and their costs determine the minimum number of turns Hongcow needs to purchase all cards.

Input

The first line of input will contain a single integer n (1 ≤ n ≤ 16).

The next n lines of input will contain three tokens ci, ri and bi. ci will be 'R' or 'B', denoting the color of the card as red or blue. ri will be an integer denoting the amount of red resources required to obtain the card, and bi will be an integer denoting the amount of blue resources required to obtain the card (0 ≤ ri, bi ≤ 107).

Output

Output a single integer, denoting the minimum number of turns needed to acquire all the cards.

Examples

Input

3
R 0 1
B 1 0
R 1 1


Output

4


Input

3
R 3 0
R 2 0
R 1 0


Output

6

Note

For the first sample, Hongcow's four moves are as follows: 

  1. Collect tokens 
  2. Buy card 1
  3. Buy card 2
  4. Buy card 3

Note, at the fourth step, Hongcow is able to buy card 3 because Hongcow already has one red and one blue card, so we don't need to collect tokens.

For the second sample, one optimal strategy is as follows: 

  1. Collect tokens 
  2. Collect tokens 
  3. Buy card 2
  4. Collect tokens 
  5. Buy card 3
  6. Buy card 1

At the fifth step, even though Hongcow has a red token, Hongcow doesn't actually need to spend it, since Hongcow has a red card already.

Solution (in JAVA):

Solution:



import java.io.*;
import java.util.*;
import java.math.*;


public class q1 {
	static int n;
	static char[] color;
	static int[] rball;
	static int[] bball;
	static int[][][] dp;
	static int w;
	static int[] maskred;
	static int[] maskblue;
	public static void main(String[] ags)
	{
		InputReader in=new InputReader(System.in);
		PrintWriter pw=new PrintWriter(System.out);
		n=in.nextInt();
		color=new char[n];
		rball=new int[n];
		bball=new int[n];
		int red=0;
		int blue=0;
		for(int i=0;i<n;i++)
		{
		 color[i]=in.readString().charAt(0);
		 rball[i]=in.nextInt();
		 bball[i]=in.nextInt();
		 red+=Math.max(0, rball[i]-n);
		 blue+=Math.max(0, bball[i]-n);
		 rball[i]=Math.min( rball[i],n);
		 bball[i]=Math.min(bball[i], n);
		 
			
		}
		int ans=Math.max(red, blue);
		 w=(int)Math.pow(2, n);
		 maskblue=new int[w];
		 maskred=new int[w];
		dp=new int[w+1][2][n*n+1];
		for(int i=0;i<w;i++)
		{
			int rr=0;
			int bb=0;
			for(int j=0;j<n;j++)
			{
				if((i & (1<<j))>0)
				{
					if(color[j]=='R')
					{
						rr++;
					}
					else
					{
						bb++;
					}
				}
			}
			maskblue[i]=bb;
			maskred[i]=rr;
		}
		for(int i=0;i<w;i++)
		{
			for(int j=0;j<2;j++)
			{
				for(int k=0;k<=n*n;k++)
				{
					dp[i][j][k]=-1;
				}
			}
		}
		int w1=Integer.MAX_VALUE;
		if(red>blue)
		{
			w1=Math.min(w1, rec(0,0,red-blue));
		}
		else
		{
			w1=Math.min(w1,rec(0,1,blue-red));
		}
		pw.println(ans+w1+n);
		
		
		
		
		pw.close();
	}
	//0 red 
	// 1 blue
	public static int rec(int mask,int z,int rem)
	{
		
		rem=Math.min(rem, n*n);
		if(dp[mask][z][rem]!=-1)
		{
			return dp[mask][z][rem];
		}
		if(mask==(w-1))
		{
			return 0;
		}
		int curred=maskred[mask];
		int curblue=maskblue[mask];
		
		int ans=Integer.MAX_VALUE;
		for(int i=0;i<n;i++)
		{
			if((mask & (1<<i))==0)
			{
					int newmask=mask | (1<<i);
					int r1=rball[i];
					int b1=bball[i];
					r1=Math.max(0, r1-curred);
					b1=Math.max(0, b1-curblue);
					if(z==0)
					{
						b1=Math.max(0, b1-rem);
						
					}
					else
					{
						r1=Math.max(0, r1-rem);
					}
					int havered=0;
					int haveblue=0;
					if(z==0)
					{
						haveblue=rem;
					}
					else
					{
						havered=rem;
					}
					int move=Math.max(r1,b1);
					int nextred=havered+move-(Math.max(0, rball[i]-curred));
					int nextblue=haveblue+move-(Math.max(0, bball[i]-curblue));
					if(nextred==0)
					{
						ans=Math.min(ans, move+rec(newmask,0,nextblue));
					}
					else
					{
						ans=Math.min(ans,move+rec(newmask,1,nextred));
					}
					
			}
		}
	
		dp[mask][z][rem]=ans;
		return ans;
		
		
		
	}
	
	
	

//	public static void dfs(int u,int p)
//	{
//		visited[u]=true;
//		for(int i=0;i<g[u].size();i++){
//			int v=g[u].get(i);
//			if(!visited[v])
//			{
//				dfs(v,u);
//			}
//		}
//	}
	public static int[] readint(InputReader in,int n)
	{
		int[] a=new int[n];
		for(int i=0;i<n;i++)
		{
			a[i]=in.nextInt();
		}
		return a;
	}
	public static long[] readlong(InputReader in,int n)
	{
		long[] a=new long[n];
		for(int i=0;i<n;i++)
		{
			a[i]=in.nextLong();
		}
		return a;
	}
	public static void addhm(HashMap<Integer,Integer> hm,int k)
	{
		int cc=0;
		if(hm.containsKey(k))
		{
			cc=hm.get(k);
		}
		hm.put(k, cc+1);
	}
	public static void removehm(HashMap<Integer,Integer> hm,int k)
	{
		int cc=hm.get(k);
		if(cc==1)
		{
			hm.remove(k);
		}
		else
		{
			hm.put(k,cc-1);
		}
	}
	
	static class InputReader {

		private InputStream stream;
		private byte[] buf = new byte[8192];
		private int curChar;
		private int snumChars;
		private SpaceCharFilter filter;

		public InputReader(InputStream stream) {
			this.stream = stream;
		}

		public int snext() {
			if (snumChars == -1)
				throw new InputMismatchException();
			if (curChar >= snumChars) {
				curChar = 0;
				try {
					snumChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (snumChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public int nextInt() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}

			int res = 0;

			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));

			return res * sgn;
		}
		
		public long nextLong() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}

			long res = 0;

			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));

			return res * sgn;
		}
		
		public String readString() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = snext();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public boolean isSpaceChar(int c) {
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public interface SpaceCharFilter {
			public boolean isSpaceChar(int ch);
		}
	}

}