fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. ios_base::sync_with_stdio(false);cin.tie(0);
  9. int n;
  10. cin>>n;
  11. vector<long long>a(n);
  12. vector<long long>b(n);
  13. for(int i=0;i<n;i++)cin>>a[i];
  14. for(int i=0;i<n;i++)cin>>b[i];
  15. sort(a.begin(),a.end());
  16. sort(b.begin(),b.end());
  17. vector<long long>::iterator low;
  18. long long p;
  19. long long sum=0;
  20. for(int i=0;i<n;i++){
  21. low=upper_bound(b.begin(),b.end(),a[i]);///ako su isti sabiram
  22. p=low-b.begin();
  23. sum+=(2*p-n)*a[i];
  24.  
  25. }
  26. for(int i=0;i<n;i++){
  27. low=upper_bound(a.begin(),a.end(),b[i]-1);///ako su isti oduzimam ih
  28. p=low-a.begin();
  29. sum+=(2*p-n)*b[i];
  30. }
  31. cout<<sum;
  32. return 0;
  33. }
Success #stdin #stdout 0s 15232KB
stdin
3
4	2	8
2	7	3
stdout
24