#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
#define FOR(i,n) for(int (i)=0;(i)<(n);(i)++)
#define FOR1(i,n) for(int (i)=1;(i)<(n);(i)++)
#define eFOR(i,n) for(int (i)=0;(i)<=(n);(i)++)
#define eFOR1(i,n) for(int (i)=1;(i)<=(n);(i)++)
#define SORT(i) sort((i).begin(),(i).end())
#define rSORT(i,a) sort((i).begin(),(i).end(),(a))
constexpr int INF = 1000000000;
constexpr ll LLINF = 1LL << 62;
constexpr int mod = 1000000007;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; }return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; }return 0; }


int main(){
    int n,m;
    cin>>n;
    priority_queue<int> a;
    vector<VI> c(n);
    FOR(i,n){
        cin>>m;
        c[i].resize(m);
        FOR(j,m)cin>>c[i][j];
    }

    ll ans=0;
    FOR(i,n){
        for(int j:c[n-i]){
            a.push(j);
            cout<<j<<endl;
        }
        cout<<endl;
        ans+=a.top();
        a.pop();
    }
    cout<<ans<<endl;
}