Advertisement
danielvitor23

Mismatched Socks

Oct 27th, 2021
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n;
  5. priority_queue<int> pq;
  6.  
  7. int main() {
  8.   cin.tie(0)->sync_with_stdio(0);
  9.  
  10.   cin >> n;
  11.   for (int i = 0, x; i < n; ++i) {
  12.     cin >> x;
  13.     pq.push(x);
  14.   }
  15.  
  16.   int64_t ans = 0;
  17.  
  18.   while (pq.size() >= 2) {
  19.     int most = pq.top(); pq.pop();
  20.     int second_most = pq.top(); pq.pop();
  21.     int max_pair = min(most, second_most);
  22.     if (max_pair > 1) max_pair /= 2;          // adicionado
  23.     most -= max_pair;
  24.     second_most -= max_pair;
  25.     ans += max_pair;
  26.     if (most > 0) pq.push(most);
  27.     if (second_most > 0) pq.push(second_most);
  28.   }
  29.  
  30.   cout << ans << '\n';
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement