fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. #define el << "\n"
  5.  
  6. int main() {
  7. ios::sync_with_stdio(false);
  8. cin.tie(nullptr);
  9. cout.tie(nullptr);
  10.  
  11. int t;
  12. cin >> t;
  13. while(t--){
  14. ll n,m;
  15. cin >> n >> m;
  16. ll a[1000005];
  17. for(ll i=0;i<n;i++){
  18. cin >> a[i];
  19. }
  20. for(ll i=0;i<n-1;i++){
  21. for(ll j=i+1;j<n;j++){
  22. if(a[j] < a[i]){
  23. ll x = a[i];
  24. a[i] = a[j];
  25. a[j] = x;
  26. }
  27. }
  28. }
  29. ll y = 0;
  30. for(ll i=0;i<n;i+=2){
  31. ll w = a[i] - a[i+1];
  32. if(w < 0){
  33. w = -w;
  34. }
  35. y += w;
  36. }
  37.  
  38. cout << y el;
  39. }
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 5320KB
stdin
3
4 10
1 4 7 9
6 20
2 5 6 11 13 15
8 100
10 12 10 15 20 18 21 25
stdout
5
10
9