fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <sstream>
  6. #include <vector>
  7. #include <map>
  8. #include <set>
  9. #include <climits>
  10. #include <tuple>
  11.  
  12. #define ll long long
  13. #define forn(i,a,b) for(ll i = a; i < b; i++)
  14. #define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  15. #define pb push_back
  16. #define MAX 1000001
  17.  
  18. using namespace std;
  19.  
  20. ll m = 0ll;
  21.  
  22. bool cmp(pair<ll,ll> &p1, pair<ll,ll> &p2)
  23. {
  24. return p1.first%m < p2.first%m;
  25. }
  26.  
  27. int main()
  28. {
  29. ll n;
  30. cin >> n >> m;
  31. ll a[n];
  32. forn(i,0,n) cin >> a[i];
  33. ll c[m];
  34. for(int i = 0; i < m; i++) c[i] = 0;
  35. vector<pair<ll,ll> > free;
  36. forn(i,0,n)
  37. {
  38. if(c[a[i]%m] == n/m)
  39. {
  40. free.push_back({a[i],i});
  41. }
  42. else
  43. {
  44. c[a[i]%m]++;
  45. }
  46. }
  47. sort(free.begin(), free.end(), cmp);
  48. ll move = 0;
  49. if(free.size() == 0)
  50. {
  51. cout << 0 << endl;
  52. forn(i,0,n) cout << a[i] << " ";
  53. return 0;
  54. }
  55. /*
  56.   forn(i,0,m)
  57.   cout << c[i] << " ";
  58.   cout << endl;
  59.   for(auto it : free)
  60.   cout << it.first << " " << it.second << endl;
  61.   */
  62. int p1 = free[0].first%m;
  63. for(int i = 0; i < free.size(); i++) /// MISTAKE IN THIS LOOP
  64. {
  65. for(; c[p1%m] == n/m; p1++, p1%=m);
  66. p1%=m;
  67. //cout << free[i].first << " " << p1 << endl;
  68. if(p1 < free[i].first%m)
  69. {
  70. move += (m-free[i].first%m + p1);
  71. a[free[i].second] += (m-free[i].first%m + p1);
  72. }
  73. else
  74. {
  75. move += (p1-free[i].first%m);
  76. a[free[i].second] += (p1 - free[i].first%m);
  77. }
  78. c[p1]++;
  79. }
  80. cout << move << endl;
  81. forn(i,0,n) cout << a[i] << " ";
  82. return 0;
  83. }
  84.  
Success #stdin #stdout 0s 4236KB
stdin
6 3
3 2 0 6 10 12
stdout
3
3 2 0 7 10 14