#include #include using namespace std; using ll = long long; #define rep(i,l,r)for(int i=(l);i<(r);i++) int main(){ int n,m; cin >> n >> m; vectorc(n); rep(i,0,n)cin >> c[i]; vectora(m); rep(i,0,m)cin >> a[i]; vector>level(m,vector(n, 1)); rep(i,0,m) { int K, x; cin >> K; rep(j, 0, K) { cin >> x; level[i][--x] = 2; } } vector>skill(n,vector(3)); rep(i,0,n)rep(j,0,2)skill[i][j+1]=2*i+j; vectorachieve(m); rep(i,0,m)achieve[i]=2*n+i; int source=2*n+m; int sink=2*n+m+1; const ll INF=(ll)1e18; atcoder::mf_graph g(2*n+m+2); rep(i,0,n)rep(j,2,2+1)g.add_edge(source,skill[i][j],c[i]); rep(i,0,m)g.add_edge(achieve[i],sink,a[i]); rep(i,0,n)rep(j,1,1+1)g.add_edge(skill[i][j],skill[i][j+1],INF); rep(i,0,m)rep(j,0,n)g.add_edge(skill[j][level[i][j]],achieve[i],INF); ll ret=g.flow(source,sink); ll ans=0; rep(i,0,m)ans+=a[i]; cout << ans-ret << endl; }