Chef and Races codechef solution in c++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> heights(n);
for (int i = 0; i < n; ++i) {
cin >> heights[i];
}
long long moves = 0;
for (int i = 0; i < n; ++i) {
if (heights[i] < k) {
moves += (k - heights[i]);
} else if (heights[i] > k) {
moves += min((heights[i] % k), (k - heights[i] % k));
}
}
cout << moves << endl;
}
return 0;
}