题目链接

https://vjudge.net/problem/CodeForces-999D

题面

Description

You are given an array consisting of n integers a1,a2,…,an, and a positive integer m. It is guaranteed that m is a divisor of n.

In a single move, you can choose any position i between 1 and n and increase ai by 1.

Let's calculate cr (0≤r≤m−1) — the number of elements having remainder r when divided by m. In other words, for each remainder, let's find the number of corresponding elements in a with that remainder.

Your task is to change the array in such a way that c0=c1=⋯=cm−1=\(\frac{n}m\).

Find the minimum number of moves to satisfy the above requirement.

Input

The first line of input contains two integers n and m (1≤n≤2⋅105,1≤m≤n). It is guaranteed that m is a divisor of n.

The second line of input contains n integers a1,a2,…,an (0≤ai≤109), the elements of the array.

Output

In the first line, print a single integer — the minimum number of moves required to satisfy the following condition: for each remainder from 0 to m−1, the number of elements of the array having this remainder equals \(\frac{n}m\)

In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed \(10^{18}\).

Examples

Input

6 3
3 2 0 6 10 12

Output

3
3 2 0 7 10 14

Input

4 2
0 1 2 3

Output

0
0 1 2 3

题解

当初比赛中做麻烦,直接写正解吧

用set维护现在个数还不够\(\frac{n}{m}\)的余数,然后从1到n循环,计算a[i]的余数,找到set中第一个大于等于这个余数的余数,把这个数的余数补成它,如果没有大于大于等于的,就找到set中的第一个数,这样每个余数不足的都由离他最近的数补成,从而使操作次数最小,用一个change数组记录每个数变化了多少,最后输出a[i]+change[i]即可

#include<bits/stdc++.h>
#define N 200050
using namespace std;
typedef long long ll;
int a[N];
int cnt[N];
int change[N];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
ll ans = 0;
set<int> s;
for (int i = 0; i < m; i++) {
s.insert(i);
}
for (int i = 1; i <= n; i++) {
int tmp = a[i] % m;
int x = tmp > *s.rbegin() ? *s.begin() : *s.lower_bound(tmp);
cnt[x]++;
if (cnt[x] == n / m) s.erase(x);
ans += (x - tmp + m) % m;
change[i] += (x - tmp + m) % m;
}
printf("%lld\n", ans);
for (int i = 1; i <= n; i++) {
printf("%d ", a[i] + change[i]);
}
return 0;
}

最新文章

  1. JavaScript第一天
  2. Python与Hack之守护进程
  3. Linux 批量替换文件名
  4. php中simplexml_load_string使用实例
  5. oracle 自增列设置
  6. 关于硬盘和几种RAID
  7. 使用AutoMapper
  8. linux模块安装卸载命令
  9. hello world是怎样运行的?
  10. Ubuntu14下LAMP环境的安装以及yaf扩展的安装
  11. 在JS中使用COM组件的方法
  12. 数据结构-环形队列 C和C++的实现
  13. 微信小程序:模板消息推送提示{“errcode”:41030,”errmsg”:”invalid page hint: [gP1eXXXXXX]”}
  14. Java开发环境Jave EE 和 jdk 下载
  15. 码农也来关注下经济问题&lt;美元加息&gt;对我们的影响
  16. JavaScript初学者必看“箭头函数”
  17. 修改Chrome启动参数解决跨域问题
  18. Misha and Palindrome Degree CodeForces - 501E (回文串计数)
  19. cobbler 自定义私有yum源
  20. js方法call和apply实例解析

热门文章

  1. 火车进出站(POJ1363)
  2. Linux操作系统下的三种Java环境配置方法
  3. treap数组版
  4. using System.Security.Cryptography
  5. 1、webpack安装
  6. C++基础 const
  7. 统计输入任意的字符中中英文字母,空格和其他字符的个数 python
  8. 安装python 第三方库遇到的安装问题 microsoft visual studio c++ 10.0 is required,Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?
  9. 14 Django的用户认证组件
  10. Servlet过滤器---登录权限控制