提交时间:2024-04-01 14:00:34

运行 ID: 140942

# include <bits/stdc++.h> using namespace std ; int a [1005] , g [1005] [1005] ; int main ( ) { int n , x , maxn , ans = -1 ; cin >> n ; for ( int i = 1 ; i <= n ; i ++ ) { cin >> a [i] ; } for ( int i = 1 ; i <= n ; i ++ ) { int x , num = 0 ; cin >> x ; string s ; getline ( cin , s ) ; for ( int j = 0 ; j < s . size ( ) ; j ++ ) { if ( '0' <= s [j] && s [j] <= '9' ) { num = num * 10 + ( s [j] - '0' ) ; } else { g [x] [num] = 1 ; num = 0 ; } } } for ( int i = n ; i >= 1 ; i -- ) { maxn = 0 ; for ( int j = i + 1 ; j <= n ; j ++ ) { if ( g [i] [j] ) { maxn = max ( maxn , a [j] ) ; } } a [i] += maxn ; ans = max ( ans , a [i] ) ; } cout << ans << endl ; return 0 ; }