#include using namespace std; using ll = long long; using ld = long double; #define rep(i, a, b) fo(int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef pair pii; typedef vector vi; const int N = 2e6 + 10; const int A = 1e6 + 1; void ProGamerMove() { int n; cin >> n; map dp; ll res = 0; for (int i = 0; i < n; ++i) { int m; cin >> m; ll bst = 0; vector> a(m); for (auto&[id, p] : a) { cin >> id >> p; if (dp.count(id)) { res = max(res, dp[id] + p); bst = max(bst, dp[id] + p); } } for (auto&[id, p] : a) { if (dp.count(id)) { dp[id] = max(dp[id], bst - p); } else dp[id] = bst - p; } } cout << res << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); int tc = 1; // cin >> tc; while(tc--) ProGamerMove(); }