#pragma GCC target("sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

ll mod = 1e9+7;

int main(){
    int N;cin >> N;
    vector<vector<int>> vec(N);
    for(int i = 0;i < N;i++){
        int b;cin >> b;
        for(int j = 0;j < b;j++){
        int q;cin >> q;vec.at(i).push_back(q);
        }
    }
    ll ans = 0;
    priority_queue<int> que;
    reverse(vec.begin(), vec.end());
    for(int i = 0;i < N;i++){
        for(auto a : vec.at(i))que.push(a);
        ans += que.top();
        que.pop();
    }
    cout<<ans<<endl;
}

