CodeForces - 1283D Christmas Trees

思路:这题就是从每个圣诞树左右进行扩展,优先近的点

做法:每个点设一个坐标id,扩展方向f(+-1),扩展距离s,把每个点放到优先队列里,按照s小的在顶端,map纪录一下扩展过的点,依次从顶端取点扩展就好了

 There are n Christmas trees on an infinite number line. The i-th tree grows at the position xi. All xi

are guaranteed to be distinct.

Each integer point can be either occupied by the Christmas tree, by the human or not occupied at all. Non-integer points cannot be occupied by anything.

There are m

people who want to celebrate Christmas. Let y1,y2,…,ym be the positions of people (note that all values x1,x2,…,xn,y1,y2,…,ym should be distinct and all yj should be integer). You want to find such an arrangement of people that the value ∑j=1mmini=1n|xi−yj|

is the minimum possible (in other words, the sum of distances to the nearest Christmas tree for all people should be minimized).

In other words, let dj

be the distance from the j-th human to the nearest Christmas tree (dj=mini=1n|yj−xi|). Then you need to choose such positions y1,y2,…,ym that ∑j=1mdj

is the minimum possible.

Input

The first line of the input contains two integers n

and m (1≤n,m≤2⋅105

) — the number of Christmas trees and the number of people.

The second line of the input contains n

integers x1,x2,…,xn (−109≤xi≤109), where xi is the position of the i-th Christmas tree. It is guaranteed that all xi

are distinct.

Output

In the first line print one integer res

— the minimum possible value of ∑j=1mmini=1n|xi−yj|

(in other words, the sum of distances to the nearest Christmas tree for all people).

In the second line print m

integers y1,y2,…,ym (−2⋅109≤yj≤2⋅109), where yj is the position of the j-th human. All yj should be distinct and all values x1,x2,…,xn,y1,y2,…,ym

should be distinct.

If there are multiple answers, print any of them.

Examples

Input

2 6
1 5

Output

8
-1 2 6 4 0 3 

Input

3 5
0 3 1

Output

7
5 -2 4 -1 2 

Sponsor

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 501050;
struct node
{
    ll id,f,s;
    friend bool operator < (node n1, node n2)
    {
        return n1.s>n2.s;//自定义优先级从大到小
    }
} a[maxn];
map<ll,ll>mp;ll n,m;
priority_queue<node>q;
ll ans[maxn];
void slove()
{
    ll s=0;
    for(ll i=1;i<=m;i++)
    {
        node u=q.top();
        q.pop();
        while(mp.count(u.id+u.f*u.s)!=0)
        {
            u=q.top();
            q.pop();
        }

        ans[i]=u.id+u.f*u.s;
        s+=u.s;
        mp[ans[i]]=1;
        //printf("i=%lld u.id=%lld u.f=%lld u.s=%lld s=%lld\n",i,u.id,u.f,u.s,s);
        u.s+=1;
        q.push(u);
        //printf("ans[%lld]=%lld\n",i,ans[i]);
    }
    printf("%lld\n",s);
    for(ll i=1;i<=m;i++)
    {
        printf("%lld",ans[i]);
        if(i==m) printf("\n");
        else printf(" ");
    }
}
int main()
{

    scanf("%lld %lld",&n,&m);
    for(ll i=1;i<=n;i++)
    {
        ll x;
        scanf("%lld",&x);
        mp[x]=1;
        q.push({x,1,1});
        q.push({x,-1,1});
    }
    slove();
}

猜你喜欢

转载自blog.csdn.net/qq_43497140/article/details/106477931