Advertisement
ec1117

Untitled

Nov 29th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef vector<int> vi;
  6. typedef pair<ll,ll> pl;
  7.  
  8. #define f first
  9. #define s second
  10.  
  11. int N,K;
  12. ll tot; // sum of cheapest
  13. vector<vi> v;
  14.  
  15. ll mx; ll res; int num;
  16.  
  17. void dfs(int pos, ll cur, int id) {
  18.     if (cur > mx || num == K) return;
  19.     res += cur; num ++;
  20.     if (id+1 < v[pos].size())
  21.         dfs(pos,cur+v[pos][id+1]-v[pos][id],id+1);
  22.     for (int i = pos+1; i < v.size(); ++i) {
  23.         ll CUR = cur+v[i][1];
  24.         if (num == K || CUR > mx) break;
  25.         dfs(i,CUR,1);
  26.     }
  27. }
  28.  
  29. void get() {
  30.     res = num = 0;
  31.     dfs(0,tot,0);
  32. }
  33.  
  34. int main() {
  35.     ios_base::sync_with_stdio(0); cin.tie(0);
  36.     freopen("roboherd.in","r",stdin);
  37.     freopen("roboherd.out","w",stdout);
  38.     cin >> N >> K;
  39.     for (int i = 0; i < N; ++i) {
  40.         int m; cin >> m;
  41.         vi p(m); for (int& x: p) cin >> x;
  42.         sort(begin(p),end(p));
  43.         tot += p[0]; for (int j = m-1; j >= 0; --j) p[j] -= p[0];
  44.         if (p.size() > 1) v.push_back(p);
  45.     }
  46.     sort(begin(v),end(v)); // sort by second-cheapest
  47.     ll lo = 0, hi = 1e13;
  48.     while (lo < hi) {
  49.         mx = (lo+hi+1)/2; get();
  50.         if (num < K) lo = mx;
  51.         else hi = mx-1;
  52.     }
  53.     mx = lo; get();
  54.     cout << res+(K-num)*(mx+1) << "\n";
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement