C. Equal Sums-set容器

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=nm

.

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 nm

.

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 1018

.

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 

题目大意: 有n个数,还有一个m要使得下面的n个数比如 a1,a2,,,an 这n个数对m取模得到的 0~m-1 种数相等。。。

这题目看得我一脸懵逼。。。。

 

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<set>
using namespace std;
#define ll long long
ll a[200000+5];     
ll cnt[200000+5];   
int main()
{
   set<ll>s;
    ll n,m;

    cin>>n>>m;
    ll num=n/m;     
    memset(cnt,0,sizeof(cnt));
    for(ll i=0;i<n;i++)
    {
        scanf("%lld",&a[i]);
        cnt[a[i]%m]++;

    }
    for(ll i=0;i<m;i++)
    {
        if(cnt[i]<num)
         s.insert(i);       
    }
    ll ans=0;              
   set<ll >::iterator it;
    for(ll i=0;i<n;i++)
    {
        if(s.size()==0)   
            break;
        ll x=a[i]%m;         
        if(cnt[x]<=num)     
            continue;
        cnt[x]--;                
        it=s.lower_bound(x);     
        if(it!=s.end())          
        {
            a[i]=a[i]+*it-x;
            ans+=*it-x;
            cnt[*it]++;
            if(cnt[*it]==num)
                s.erase(*it);
        }
        else
        {
            a[i]=a[i]+m-x+*s.begin();
            ans=ans+m-x+*s.begin();
            cnt[*s.begin()]++;
            if(cnt[*s.begin()]==num)
                s.erase(*s.begin());

        }
    }
    cout<<ans<<endl;
    for(ll i=0; i<n; i++)
            printf("%lld ",a[i]);

    return 0;
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值