Mahmoud_Hawara

Untitled

Jul 25th, 2022
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #pragma GCC optimize("-Ofast")
  4. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")
  5. #pragma GCC optimize("-ffast-math")
  6. #pragma GCC optimize("-funroll-loops")
  7. #pragma GCC optimize("-funroll-all-loops,-fpeel-loops,-funswitch-loops")
  8. #define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  9. const long long MOD = 1e9 + 7, OO = 1e18;
  10. const double PI = acos(-1);
  11. const int N = 1e6 + 5;
  12. const int dx[4] = {0, 0, 1, -1};
  13. const int dy[4] = {1, -1, 0, 0};
  14.  
  15. long long n, k, x;
  16.  
  17. int main()
  18. {
  19.     IO
  20.     while (cin >> n, n)
  21.     {
  22.         long long ans = 0;
  23.         multiset<long long>s;
  24.         while (n--)
  25.         {
  26.             cin >> k;
  27.             while (k--)
  28.             {
  29.                 cin >> x;
  30.                 s.insert(x);
  31.             }
  32.             long long mn = *s.begin();
  33.             long long mx = *s.rbegin();
  34.             ans += mx - mn;
  35.             s.erase(s.begin());
  36.             s.erase(s.find(mx));
  37.         }
  38.         cout << ans << '\n';
  39.     }
  40.     return 0;
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment