#include #if __has_include() #endif using namespace std; #define eb emplace_back #define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__) #define FO(n) for(ll IJK=n;IJK-->0;) #define fo(i,...) for(auto[i,i##stop,i##step]=for_range(0,__VA_ARGS__);ivoid pp(const auto&...a){[[maybe_unused]]const char*c="";((o<(a...);} #define entry defpp void main();void main2();}int main(){my::io();my::main();}namespace my{ namespace my{ void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<constexpr auto for_range(T s,T b){T a=0;if(s)swap(a,b);return array{a-s,b,1-s*2};} void lin(auto&...a){(cin>>...>>a);} constexpr ll size10(auto x){x|=1;ll r=0;while(x>0)x/=10,++r;return r;} templateconstexpr ll maxsize10(){return size10(numeric_limits::max());} bool amax(auto&a,const auto&b){return astruct infinity{ templatestatic constexpr T ones(size_t n){return n?ones(n-1)*10+1:0;} templateconstexpr operator T()const{static constexpr T v=ones(maxsize10())*(1-is_negative*2);return v;} constexpr auto operator-()const{return infinity();} }; constexpr infinity oo; templateusing pack_back_t=tuple_element_t>; } namespace my{ templatestruct vec; templatestruct hvec_helper{using type=vec::type>;}; templatestruct hvec_helper<0,T>{using type=T;}; templateusing hvec=hvec_helper::type; templatestruct vec:vector{ using vector::vector; auto&emplace_back(auto&&...a){vector::emplace_back(std::forward(a)...);return*this;} }; templaterequires(sizeof...(A)>=2)vec(const A&...a)->vec>>; } namespace my{entry void main(){ LL(N,K); vec>g(N); veca(N); fo(i,N){ lin(a[i]); LL(M); FO(M){ LL(p);--p; g[p].eb(i); } } vec dp(N,ll(-oo)); dp[0]=0; fo(i,N-1){ amax(dp[i+1],dp[i]); fe(g[i],j)amax(dp[j],dp[i]+a[j]-a[i]); } pp(dp[N-1]); }}