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 CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;
int a[70005][3][300];
struct info {
  int red, blue;
  int xx;
} b[20], d[20];
vector<int> g[20];
int i, j, k, l, m, n, r, tot, ans;
int sum[3];
char c;
int xl, xr;
int main() {
  l = 0;
  scanf("%d\n", &n);
  m = n * n;
  for (i = 1; i <= n; i++) {
    scanf("%c %d %d\n", &c, &b[i].red, &b[i].blue);
    if (c == 'R')
      b[i].xx = 1;
    else
      b[i].xx = 2;
    if (b[i].red >= n || b[i].blue >= n) {
      xl += max(0, b[i].red - n);
      xr += max(0, b[i].blue - n);
      b[i].red = min(n, b[i].red);
      b[i].blue = min(n, b[i].blue);
    }
  }
  for (i = 0; i < (1 << n); i++) {
    j = i;
    k = 0;
    while (j > 0) {
      if (j & 1) k++;
      j >>= 1;
    }
    g[k].push_back(i);
  }
  for (i = 0; i < (1 << n); i++)
    for (j = 0; j <= n * n; j++) a[i][1][j] = a[i][2][j] = 1e9;
  if (xl > xr)
    a[0][1][min(xl - xr, n * n)] = 0;
  else
    a[0][2][min(xr - xl, n * n)] = 0;
  if (xl == xr) a[0][1][0] = a[0][2][0] = 0;
  for (int w = 1; w <= n; w++) {
    for (i = 0; i < g[w].size(); i++) {
      int now = g[w][i];
      sum[1] = sum[2] = 0;
      for (j = 0; j < n; j++)
        if ((now & (1 << j))) sum[b[j + 1].xx]++;
      for (j = 0; j < n; j++) {
        if ((now & (1 << j)) == 0) continue;
        l = sum[1];
        r = sum[2];
        if (b[j + 1].xx == 1)
          l--;
        else
          r--;
        int la = now ^ (1 << j);
        for (k = 0; k <= n * n; k++) {
          if (a[la][1][k] < 1e9) {
            int nl = 0, nr = k;
            int nred = max(0, b[j + 1].red - l - nl);
            int nblue = max(0, b[j + 1].blue - r - nr);
            tot = max(nred, nblue);
            int re = tot + nl - max(0, b[j + 1].red - l);
            int bl = tot + nr - max(0, b[j + 1].blue - r);
            if (re == 0)
              a[now][1][min(bl, n * n)] =
                  min(a[now][1][min(bl, n * n)], a[la][1][k] + tot);
            if (bl == 0)
              a[now][2][min(re, n * n)] =
                  min(a[now][2][min(re, n * n)], a[la][1][k] + tot);
          }
          if (a[la][2][k] < 1e9) {
            int nl = k, nr = 0;
            int nred = max(0, b[j + 1].red - l - nl);
            int nblue = max(0, b[j + 1].blue - r - nr);
            tot = max(nred, nblue);
            int re = tot + nl - max(0, b[j + 1].red - l);
            int bl = tot + nr - max(0, b[j + 1].blue - r);
            if (re == 0)
              a[now][1][min(bl, n * n)] =
                  min(a[now][1][min(bl, n * n)], a[la][2][k] + tot);
            if (bl == 0)
              a[now][2][min(re, n * n)] =
                  min(a[now][2][min(re, n * n)], a[la][2][k] + tot);
          }
        }
      }
    }
  }
  l = r = 0;
  k = (1 << n) - 1;
  ans = 1e9;
  for (i = 0; i <= n * n; i++) {
    ans = min(ans, a[k][1][i]);
    ans = min(ans, a[k][2][i]);
  }
  printf("%d", ans + n + max(xl, xr));
  return 0;
}