#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

int main() {
  i64 N;
  cin >> N;
  vector<i64> M(N);
  vector<vector<i64>> c(N);
  for(i64 i = 0;i < N;i++) {
    cin >> M[i];
    c[i].resize(M[i]);
    for(int j = 0;j < M[i];j++) {
      cin >> c[i][j];
    }
  }
  priority_queue<i64> que;

  i64 ans = 0;

  for(i64 i = N - 1;i >= 0;i--) {
    for(auto x: c[i]) {
      que.push(x);
    }
    if(!que.empty()) {
      ans += que.top();
      que.pop();
    }
  }
  cout << ans << endl;
}

