#include #include #include #define _TST 0 using namespace std; int main() { long long num,dist,pom; std::vector s; cin >> num >> dist; for(long long i = 0 ; i < num ; ++i) { cin >> pom; s.push_back(pom); } if(num == 1) { cout << 0 << endl; return 0; } sort(s.begin(),s.end()); bool suda = (num %2 == 0)?true:false; long long pivot_add =num/2 -((suda) ? 1 : 0) ; long long pivot = s[pivot_add+_TST]; long long exp,res = 0; // exp = pivot; exp = pivot - dist * pivot_add; for (auto& n : s) { res += abs(n - exp); exp += dist; } //za pivotem vcetne //for(auto it = pivot_add + _TST ; it < s.size() ; it++) //{ // res += abs(s[it] - exp); // potrebuje kladna cisla // exp += dist; //} //exp = pivot-dist; ////pred //for(auto it = pivot_add-1 + _TST; it >=0 ; it--) //{ // res += abs(s[it] - exp); // potrebuje kladna cisla // exp -= dist; //} cout << res << endl; return 0; }