#include <bits/stdc++.h>
using namespace std;
#define fastio \
ios::sync_with_stdio(false); \
cin.tie(nullptr);
#define s(x) x.size()
#define sort(x) sort(x.begin(), x.end())
#define rsort(x) sort(x.rbegin(), x.rend())
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define INF 1e9
#define INFL 1e18
using ll = long long;
using pii = pair<int, int>;
vector<int> v;
int t;
ll how_much(int sr)
{
int n = v.size();
ll s = 0;
for (int i = 0; i < n; i++)
{
s += min(abs(v[i] - sr) % t, abs(t - abs(v[i] - sr) % t));
}
return s;
}
void solve()
{
int n;
cin >> n >> t;
v.resize(n);
for (int i = 0; i < n; i++)
{
cin >> v[i];
v[i] % t;
}
int l = 0, r = t;
ll poprzedni = how_much((l + r+1) / 2 + 1);
ll minn = 1e18;
for (int i = 0; i <= t; i++)
{
minn=min(minn,how_much(i) );
}
// while (l < r)
// {
// cout << l << ' ' << r << endl;
// int sr = (l + r+1) / 2;
// ll wynik = how_much(sr);
// if (wynik < poprzedni)
// l = sr;
// else
// r = sr - 1;
// }
cout << minn << endl;
}
int main()
{
fastio;
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |