Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef vector<int> vi;
- typedef pair<ll,ll> pl;
- #define f first
- #define s second
- int N,K;
- ll tot; // sum of cheapest
- vector<vi> v;
- ll mx; ll res; int num;
- void dfs(int pos, ll cur, int id) {
- if (cur > mx || num == K) return;
- res += cur; num ++;
- if (id+1 < v[pos].size())
- dfs(pos,cur+v[pos][id+1]-v[pos][id],id+1);
- for (int i = pos+1; i < v.size(); ++i) {
- ll CUR = cur+v[i][1];
- if (num == K || CUR > mx) break;
- dfs(i,CUR,1);
- }
- }
- void get() {
- res = num = 0;
- dfs(0,tot,0);
- }
- int main() {
- ios_base::sync_with_stdio(0); cin.tie(0);
- freopen("roboherd.in","r",stdin);
- freopen("roboherd.out","w",stdout);
- cin >> N >> K;
- for (int i = 0; i < N; ++i) {
- int m; cin >> m;
- vi p(m); for (int& x: p) cin >> x;
- sort(begin(p),end(p));
- tot += p[0]; for (int j = m-1; j >= 0; --j) p[j] -= p[0];
- if (p.size() > 1) v.push_back(p);
- }
- sort(begin(v),end(v)); // sort by second-cheapest
- ll lo = 0, hi = 1e13;
- while (lo < hi) {
- mx = (lo+hi+1)/2; get();
- if (num < K) lo = mx;
- else hi = mx-1;
- }
- mx = lo; get();
- cout << res+(K-num)*(mx+1) << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement