fork download
  1. //NiceDuck
  2. #include "bits/stdc++.h"
  3. typedef long long ll;
  4. using namespace std;
  5. #define FILE "000"
  6. #define foru(i,a,b) for(int i=(int)(a); i<=(int)(b); ++i)
  7. #define ford(i,a,b) for(int i=(int)(a); i>=(int)(b); --i)
  8. #define fastio ios_base::sync_with_stdio(0);cin.tie(0);
  9. #define pb push_back
  10. #define fi first
  11. #define se second
  12. #define pii pair<int,int>
  13. #define pil pair<int,ll>
  14. #define pli pair<ll,int>
  15. #define MOD 1000000007
  16. #define el "\n"
  17.  
  18.  
  19. ll calc(int n, int m, ll a[], ll b[])
  20. {
  21. sort(a+1,a+n+1); sort(b+1,b+m+1);
  22. if(n==1) return max(abs(a[1]-b[1]),abs(a[1]-b[m]));
  23. ll ans=0;
  24. int i=1,j=n,i1=1,j1=m;
  25. while(i<=j)
  26. {
  27. int x=abs(a[i]-b[j1]), y=abs(a[j]-b[i1]);
  28. if(x>=y)
  29. {
  30. ans+=x;
  31. ++i; --j1;
  32. }
  33. else
  34. {
  35. ans+=y;
  36. --j; ++i1;
  37. }
  38. }
  39. return ans;
  40. }
  41.  
  42. int main()
  43. {
  44. fastio
  45. #ifndef ONLINE_JUDGE
  46. freopen(FILE ".inp","r",stdin);
  47. freopen(FILE ".out","w",stdout);
  48. #endif // ONLINE_JUDGE
  49.  
  50. int t; cin>>t;
  51. while(t--)
  52. {
  53. int n,m; cin>>n>>m;
  54. ll a[n+3], b[m+3];
  55. foru(i,1,n) cin>>a[i];
  56. foru(i,1,m) cin>>b[i];
  57. cout<<calc(n,m,a,b)<<el;
  58. }
  59.  
  60.  
  61. return 0;
  62. }
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
Standard output is empty