fork download
  1. #include <stdio.h>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. int main() {
  6. int n;
  7. scanf("%d", &n);
  8.  
  9. std::vector<int> a(n), b(n);
  10. for (int i = 0; i < n; ++i) {
  11. scanf("%d %d", &a[i], &b[i]);
  12. }
  13.  
  14. std::vector<int> queue1(n), queue2(n);
  15.  
  16. for (int i = 0; i < n; ++i) {
  17. queue1[i] = i;
  18. queue2[i] = i;
  19. }
  20.  
  21. std::sort(queue1.begin(), queue1.end(), [&](const int i, const int j) {
  22. return a[i] > a[j] || (a[i] == a[j] && b[i] > b[j]);
  23. });
  24.  
  25. std::sort(queue2.begin(), queue2.end(), [&](const int i, const int j) {
  26. return b[i] > b[j] || (b[i] == b[j] && a[i] > a[j]);
  27. });
  28.  
  29. std::vector<bool> actual(n, true);
  30.  
  31. int sum1 = 0, sum2 = 0, pos1 = 0, pos2 = 0;
  32. while (true) {
  33. while (pos1 < n && actual[pos1] == false) ++pos1;
  34. if (pos1 == n) break;
  35. sum1 += a[pos1];
  36. actual[pos1] = false;
  37. while (pos2 < n && actual[pos2] == false) ++pos2;
  38. if (pos2 == n) break;
  39. sum2 += b[pos2];
  40. actual[pos2] = false;
  41. }
  42. printf("%d", sum1-sum2);
  43. return 0;
  44. }
Success #stdin #stdout 0s 4364KB
stdin
3
1 2
3 4
5 6
stdout
2