fork download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. vector <long long> V;
  10.  
  11. int main()
  12. {
  13. int n, m;
  14. scanf("%d %d", &n, &m);
  15. unsigned long long ans = 0;
  16. long long x;
  17. for (int i=1; i<=n; i++)
  18. {
  19. scanf("%lld", &x);
  20. V.push_back(x);
  21. }
  22. long long med = V[n/2];
  23. int pos = 0;
  24. while (pos < n/2)
  25. {
  26. ans += (abs(V[pos]-med) * 2);
  27. pos += m;
  28. }
  29. pos = V.size()-1;
  30. while (pos > n/2)
  31. {
  32. ans += (abs(V[pos]-med) * 2);
  33. pos -= m;
  34. }
  35. printf("%lld\n", ans);
  36. return 0;
  37. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
0