#include using namespace std; struct iofast_t { iofast_t() { ios::sync_with_stdio(false); cin.tie(nullptr); } } iofast; struct uns_t {} uns; template auto vec(Element init, Head arg, Args ...args) { if constexpr (sizeof...(Args) == 0) return std::vector(arg, init); else return std::vector(arg, vec(init, args...)); } template auto vec(uns_t, Head arg, Args ...args) { return vec(Element(), arg, args...); } template > T &chmin(T &l, T r, Compare &&f = less()) { return l = min(l, r, f); } template > T &chmax(T &l, T r, Compare &&f = less()) { return l = max(l, r, f); } int main() { int n, k; cin >> n >> k; auto a = vec(uns, n); auto g = vec(uns, n, 0); for (int i = 0; i < n; ++i) { int m; cin >> a[i] >> m; while (m--) { int b; cin >> b; --b; g[b].push_back(i); } } auto dp = vec(0, n + 1); for (int i = n - 1; 0 <= i; --i) { dp[i] = dp[i + 1]; for (auto v : g[i]) { chmax(dp[i], dp[v] + (a[v] - a[i])); } } cout << dp[0] << endl; }