Problem: The Hexadecimal virus loves playing with number sets — intersecting them, uniting them. One beautiful day she was surprised to find out that Scuzzy, her spherical pet cat, united all sets in one and ate the result! Something had to be done quickly and Hexadecimal rushed to the market.

The market has n sets of numbers on sale. The virus wants to buy the following collection of sets: the number of sets in the collection should be exactly the same as the number of numbers in the union of all bought sets. Moreover, Hexadecimal wants to buy the cheapest suitable collection of set.

Yet nothing's so easy! As Mainframe is a kingdom of pure rivalry markets, we know that the union of any k sets contains no less than k distinct numbers (for every positive integer k).

Help the virus choose the suitable collection of sets. The collection can be empty.

Input

The first line contains the only number n (1 ≤ n ≤ 300) — the number of sets available in the market.

Next n lines describe the goods: first we are given mi (1 ≤ mi ≤ n) — the number of distinct numbers in the i-th set, then follow mi numbers — the set's elements. We know that the set's elements are distinct positive integers and they do not exceed n.

The last line contains n integers whose absolute values do not exceed 106 — the price of each set.

Output

Print a single number — the minimum price the virus will have to pay for such a collection of k sets that union of the collection's sets would have exactly k distinct numbers (<image>).

Examples

Input

3
1 1
2 2 3
1 3
10 20 -3


Output

-3


Input

5
2 1 2
2 2 3
2 3 4
2 4 5
2 5 1
1 -1 1 -1 1


Output

0


Input

5
2 1 2
2 2 3
2 3 4
2 4 5
2 5 1
-1 1 -1 1 -1


Output

-1

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
class IO {
 protected:
  static const int BSIZE = 65536;
  int is;
  char ib[BSIZE], ob[BSIZE], *ip, *op;
  FILE *in, *out;

 public:
  inline IO(FILE *in = stdin, FILE *out = stdout) {
    ip = ib + BSIZE, op = ob, is = 0;
    this->in = in;
    this->out = out;
  }
  inline ~IO() { fwrite(ob, 1, op - ob, out); }
  inline char getchar() {
    if (ip == ib + BSIZE) is = fread(ib, 1, BSIZE, in), ip = ib;
    return ip == ib + is ? 0 : *ip++;
  }
  inline void putchar(char c) {
    if (op == ob + BSIZE) fwrite(ob, 1, BSIZE, out), op = ob;
    *op++ = c;
  }
};
class IOX : public IO {
 protected:
  int tmp[64];

 public:
  inline IOX(FILE *in = stdin, FILE *out = stdout) : IO(in, out) {}
  inline int getdigit() {
    register char ch = getchar();
    while (!isdigit(ch)) ch = getchar();
    return ch ^ 48;
  }
  inline char getalpha() {
    register char ch = getchar();
    while (!isalpha(ch)) ch = getchar();
    return ch;
  }
  inline int getint() {
    register int x = 0, f = 0;
    register char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) f ^= ch == 45;
    for (; isdigit(ch); ch = getchar()) x = (((x << 2) + x) << 1) + (ch ^ 48);
    return f ? -x : x;
  }
  inline unsigned getuint() {
    register unsigned x = 0;
    register char ch = getchar();
    for (; isdigit(ch); ch = getchar()) x = (((x << 2) + x) << 1) + (ch ^ 48);
    return x;
  }
  inline long long getint64() {
    register long long x = 0, f = 0;
    register char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) f ^= ch == 45;
    for (; isdigit(ch); ch = getchar()) x = (((x << 2) + x) << 1) + (ch ^ 48);
    return f ? -x : x;
  }
  inline unsigned long long getuint64() {
    register unsigned long long x = 0;
    register char ch = getchar();
    for (; isdigit(ch); ch = getchar()) x = (((x << 2) + x) << 1) + (ch ^ 48);
    return x;
  }
  inline void put(int x) {
    if (!x) return putchar('0');
    if (x < 0) putchar(45), x = -x;
    register int _6;
    for (_6 = 0; x; x /= 10) tmp[++_6] = (x % 10) ^ 48;
    while (_6) putchar(tmp[_6--]);
  }
  inline void put(long long x) {
    if (!x) return putchar('0');
    if (x < 0) putchar(45), x = -x;
    register int _6;
    for (_6 = 0; x; x /= 10) tmp[++_6] = (x % 10) ^ 48;
    while (_6) putchar(tmp[_6--]);
  }
  inline void put(const char *s) {
    for (; *s; s++) putchar(*s);
  }
  inline void put(char c) { putchar(c); }
  template <typename T, typename... Args>
  inline void put(T first, Args... rest) {
    put(first), put(rest...);
  }
  inline void puts(const char *s) {
    put(s);
    putchar(10);
  }
} io;
namespace HLPPImpl {
const long long INF = (~0ULL) >> 2;
int head[310], to[50010 << 1], next[50010 << 1], cap[50010 << 1], tot = 0;
inline void $(int u, int v, int w) {
  next[tot] = head[u], to[tot] = v, cap[tot] = w, head[u] = tot++;
  next[tot] = head[v], to[tot] = u, cap[tot] = 0, head[v] = tot++;
}
int n, s, t, h[310], gap[310], inq[310];
long long prs[310];
inline int HLPP() {
  std::priority_queue<std::pair<int, int>> Q;
  h[s] = n;
  prs[s] = INF;
  inq[s] = 1;
  Q.emplace(h[s], s);
  gap[0] = n - 1;
  while (Q.size()) {
    int x = Q.top().second;
    Q.pop();
    if (!prs[x] || x == t) continue;
    for (int i = head[x]; ~i; i = next[i]) {
      if (x == s || h[to[i]] + 1 == h[x]) {
        int flow = std::min(prs[x], (long long)cap[i]);
        if (flow) {
          cap[i] -= flow, cap[i ^ 1] += flow, prs[x] -= flow,
              prs[to[i]] += flow;
          if (to[i] != s && !inq[to[i]]) {
            Q.emplace(h[to[i]], to[i]);
            inq[to[i]] = 1;
          }
          if (!prs[x]) break;
        }
      }
    }
    if (x != s && prs[x]) {
      if (!--gap[h[x]]) {
        int th = h[x];
        for (int i = 1; i <= n; i++) {
          if (i != s && i != t && h[i] >= th) h[i] = n, prs[i] = 0;
        }
        continue;
      }
      h[x] = n;
      for (int i = head[x]; ~i; i = next[i]) {
        if (cap[i]) {
          h[x] = std::min(h[x], h[to[i]] + 1);
        }
      }
      ++gap[h[x]];
      Q.emplace(h[x], x);
    } else {
      inq[x] = 0;
    }
  }
  return prs[t];
}
}  // namespace HLPPImpl
std::vector<int> go[310];
int n = io.getint(), ans = 0, match[310], vis[310];
int dfs(int x) {
  for (int y : go[x]) {
    if (vis[y]) continue;
    vis[y] = 1;
    if (!match[y] || dfs(match[y])) return match[y] = x, 1;
  }
  return 0;
}
int main() {
  for (int i = 1; i <= n; i++) {
    for (int j = io.getint(); j; j--) {
      go[i].push_back(io.getint());
    }
  }
  for (int i = 1; i <= n; i++) memset(vis, 0, sizeof(vis)), dfs(i);
  memset(HLPPImpl::head, -1, sizeof(HLPPImpl::head));
  HLPPImpl::s = n + 1, HLPPImpl::t = HLPPImpl::n = n + 2;
  for (int i = 1; i <= n; i++) {
    int x = -io.getint();
    if (x > 0) {
      ans += x;
      HLPPImpl::$(HLPPImpl::s, i, x);
    } else if (x < 0) {
      HLPPImpl::$(i, HLPPImpl::t, -x);
    }
    for (auto j : go[i]) {
      if (match[j] != i) {
        HLPPImpl::$(i, match[j], 0x3F3F3F3F);
      }
    }
  }
  io.put(HLPPImpl::HLPP() - ans, '\n');
  return 0;
}