#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* macro */ #define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++) #define rrep(i, a, n) for (int i = ((int)(n-1)); i >= (int)(a); i--) #define Rep(i, a, n) for (i64 i = (i64)(a); i< (i64)(n); i++) #define RRep(i, a, n) for (i64 i = ((i64)(n-i64(1))); i>=(i64)(a); i--) #define all(v) (v).begin(),(v).end() #define rall(v) (v).rbegin(),(v).rend() #define Bit(n) (1LL<<(n)) /* macro end */ /* template */ namespace ebi { #ifdef LOCAL #define debug(...) \ std::cerr << "LINE: " << __LINE__ << " [" << #__VA_ARGS__ << "]:", \ debug_out(__VA_ARGS__) #else #define debug(...) #endif void debug_out() { std::cerr << std::endl; } template void debug_out(Head h, Tail... t) { std::cerr << " " << h; if (sizeof...(t) > 0) std::cout << " :"; debug_out(t...); } template std::ostream& operator<<(std::ostream& os, std::pair pa) { return os << pa.first << " " << pa.second; } template std::ostream& operator<<(std::ostream& os, std::vector vec) { for (std::size_t i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } std::ostream& operator<<(std::ostream& os, atcoder::modint1000000007 a){ return os << a.val(); } std::ostream& operator<<(std::ostream& os, atcoder::modint998244353 a){ return os << a.val(); } using size_t = std::size_t; using i32 = std::int32_t; using u32 = std::uint32_t; using i64 = long long; using u64 = unsigned long long; template void fill(std::vector &v) { for(T &e: v) std::cin >> e; } template struct Edge { int to; T cost; Edge(int to, T cost) : to(to), cost(cost) { } }; template std::ostream& operator<<(std::ostream& os, Edge e) { return os << "[" << e.to << ", " << e.cost << "] "; } template struct Graph : std::vector>> { using std::vector>>::vector; void add_edge(int u, int v, T w, bool directed = false) { (*this)[u].emplace_back(v, w); if(directed) return; (*this)[v].emplace_back(u, w); } }; struct graph : std::vector> { using std::vector>::vector; void add_edge(int u, int v, bool directed = false) { (*this)[u].emplace_back(v); if(directed) return; (*this)[v].emplace_back(u); } }; template inline bool chmin(T &a, T b) { if (a > b){ a = b; return true; } return false; } template inline bool chmax(T &a, T b) { if (a < b){ a = b; return true; } return false; } template T gcd(T a, T b){ if( b==0 ) return a; else return gcd(b, a%b); } template T lcm(T a, T b) { return (a*b)/gcd(a,b); } template T pow(T x, i64 n) { T res = 1; while(n>0){ if(n&1) res = res *x; x = x*x; n>>=1; } return res; } template T mod_pow(T x, i64 n, i64 mod) { T res = 1; while(n > 0) { if(n & 1) res = (res * x)%mod; x = (x*x)%mod; n >>= 1; } return res; } template T scan() { T val; std::cin >> val; return val; } constexpr i64 LNF = std::numeric_limits::max()/2; constexpr int INF = std::numeric_limits::max()/2; const long double PI = acos(-1); const std::vector dy = {1,0,-1,0,1,1,-1,-1}; const std::vector dx = {0,1,0,-1,1,-1,1,-1}; } /* template end */ namespace ebi { using pii = std::pair; using ld = long double; using mint = atcoder::modint1000000007; void main_() { int n,k; std::cin >> n >> k; std::vector dp(n+1, -LNF); std::vector a(n); dp[0] = 0; i64 ans = 0; rep(i,0,n) { int m; std::cin >> a[i] >> m; chmax(dp[i+1], ans-a[i]); rep(j,0,m) { int b; std::cin >> b; chmax(dp[i+1], dp[b]); } chmax(ans, dp[i+1] + a[i]); } std::cout << ans << std::endl; } } int main() { std::cout << std::fixed << std::setprecision(15); std::cin.tie(nullptr); std::ios::sync_with_stdio(false); int t = 1; while(t--) { ebi::main_(); } return 0; }