E. Yet Another Division Into Teams

There are n students at your university. The programming skill of the i-th student is ai. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has 2⋅105 students ready for the finals!

Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of k students with programming skills a[i1],a[i2],…,a[ik], then the diversity of this team is maxj=1ka[ij]−minj=1ka[ij]).

The total diversity is the sum of diversities of all teams formed.

Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.

Input

The first line of the input contains one integer n (3≤n≤2⋅105) — the number of students.

The second line of the input contains n integers a1,a2,…,an (1≤ai≤109), where ai is the programming skill of the i-th student.

Output

In the first line print two integers res and k — the minimum total diversity of the division of students and the number of teams in your division, correspondingly.

In the second line print n integers t1,t2,…,tn (1≤ti≤k), where ti is the number of team to which the i-th student belong.

If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.

Examples

input

5

1 1 3 4 2

output

3 1

1 1 1 1 1

input

6

1 5 12 13 2 15

output

7 2

2 2 1 1 2 1

input

10

1 2 5 129 185 581 1041 1909 1580 8150

output

7486 3

3 3 3 2 2 2 2 1 1 1

Note

In the first example, there is only one team with skills [1,1,2,3,4] so the answer is 3. It can be shown that you cannot achieve a better answer.

In the second example, there are two teams with skills [1,2,5] and [12,13,15] so the answer is 4+3=7.

In the third example, there are three teams with skills [1,2,5], [129,185,581,1041] and [1580,1909,8150] so the answer is 4+912+6570=7486.

题意

这个学校里面有n个学生,你需要给他们分成若干的队伍,每个队伍最少3个人。

每个队伍定义差异值是这个队伍最强的人和最弱的人的能力值差。

现在你需要构建若干个队伍,使得差异值的总和最小。

题解

我们先排序,那么分队伍一定是选择排序后的连续几个人组成一队。

然后每个队伍一定人数最多为5个人,因为6个人就可以拆成两队,然后两队的代价一定是比一个队伍的代价小。

然后就是个简单的dp了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int n;
pair<int,int> k[maxn];
int dp[maxn];
int p[maxn];
int fr[maxn];
int ans_pos[maxn];
int tot=0;
void dfs(int x){
if(x==0)return;
tot++;
p[x]=1;
dfs(fr[x]);
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&k[i].first);
k[i].second=i;
}
sort(k+1,k+1+n);
memset(dp,-1,sizeof(dp));
dp[0]=0;
dp[3]=k[3].first-k[1].first;
for(int i=4;i<=n;i++){
for(int j=3;j<=6;j++){
if(dp[i-j]!=-1){
if(dp[i]==-1){
dp[i]=dp[i-j]+k[i].first-k[i-j+1].first;
fr[i]=i-j;
}
else{
if(dp[i-j]+(k[i].first-k[i-j+1].first)<dp[i]){
fr[i]=i-j;
dp[i]=dp[i-j]+k[i].first-k[i-j+1].first;
}
}
}
}
} dfs(n);
cout<<dp[n]<<" "<<tot<<endl;
int tot2=1;
for(int i=1;i<=n;i++){
if(p[i]==0){
p[i]=tot2;
}else if(p[i]==1){
p[i]=tot2;
tot2++;
}
}
for(int i=1;i<=n;i++){
ans_pos[k[i].second]=p[i];
}
for(int i=1;i<=n;i++){
cout<<ans_pos[i]<<" ";
}
cout<<endl;
}

Codeforces Round #598 (Div. 3) E. Yet Another Division Into Teams dp的更多相关文章

  1. Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划

    Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划 [Problem Description] 给你\( ...

  2. 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】

    https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...

  3. Codeforces Round #396 (Div. 2) A B C D 水 trick dp 并查集

    A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...

  4. Codeforces Round #598 (Div. 3)E(dp路径转移)

    题:https://codeforces.com/contest/1256/problem/E 题意:给一些值,代表队员的能力值,每组要分3个或3个以上的人,然后有个评价值x=(队里最大值-最小值), ...

  5. Codeforces Round #598 (Div. 3)

    传送门 A. Payment Without Change 签到. Code /* * Author: heyuhhh * Created Time: 2019/11/4 21:19:19 */ #i ...

  6. Codeforces Round #598 (Div. 3) F. Equalizing Two Strings 构造

    F. Equalizing Two Strings You are given two strings s and t both of length n and both consisting of ...

  7. Codeforces Round #598 (Div. 3) D. Binary String Minimizing 贪心

    D. Binary String Minimizing You are given a binary string of length n (i. e. a string consisting of ...

  8. Codeforces Round #598 (Div. 3) C. Platforms Jumping 贪心或dp

    C. Platforms Jumping There is a river of width n. The left bank of the river is cell 0 and the right ...

  9. Codeforces Round #598 (Div. 3) B. Minimize the Permutation 贪心

    B. Minimize the Permutation You are given a permutation of length n. Recall that the permutation is ...

随机推荐

  1. Electron npm install 常见错误(Linux)

    Linux版本 Ubuntu 12.04 (32bit) 安装Git sudo apt-get install git 生成ssh key #查看有没有sshkey cd ~/.ssh #生成 ssh ...

  2. c++11 auto 与auto& 遍历vector区别

    目录 说明 c++11 auto 与auto& 遍历区别 今天被这个问题坑了一天,一直以为是算法错了,debug了一天,最后暴力生成数据才发现,测试代码如下: 说明 转载请注明出处:https ...

  3. VMware安装vmtools实现宿主机和虚拟机共享粘贴板

    打开VMware以Ubuntu14.04.6为例,保持网络畅通,在线下载工具包 下载完成后dvd下出现该压缩包 将其复制到桌面并在桌面进入终端 执行命令: tar -zxvf xxxx.tar.gz ...

  4. OPENGL 坐标轴转换

    坐标轴 平移 旋转 缩放 重置坐标轴 矩阵操作 示例 1.坐标轴  OpenGL 使用的右手坐标系,从正面看原点,逆时针旋转被认为是正旋转. x轴:从左到右 y轴:从底部向上 z轴:从屏幕背向朝向前方 ...

  5. Liu Junqiao:工作中用到的命令以及问题汇总

    工作中用到的命令以及问题汇总 2019-11-29 查看系统运行时间,这个问题是因为我们在阿里云上有个机器,在某一天发现这台机器上有的服务莫名奇妙的停了,然后排查时怀疑机器被重启过用如下如下命令查看了 ...

  6. JSON.parse和JSON.stringify方法

    1.JSON.parse(“要转换的数据”) 2.JSON.stringify(“要转化的数据”) 3.首先,两个方法的用法是有差别的: ①.JSON.stringify是从一个对象中解析出字符串 ② ...

  7. tensorflow基础-数据类型

    一:tensorflow中的计算定义和执行 首先,对于tensorflow来说,最重要的概念就是图(Graph)和会话(Session),tensorflow的计算思想是:以图的形式来表示模型,表示和 ...

  8. 修改项目语言为C#8.0

    错误 CS8370 功能“Using 声明”在 C# 7.3 中不可用.请使用 8.0 或更高的语言版本.  用记事本打开项目文件 XXX.csproj,找到LangVersion,修改为8.0. 如 ...

  9. .NET多线程知识快速学习

    多线程是一个不会过时的话题,因为每个开发的成长必然要掌握这个知识点,否则半懂不懂怎么保证系统的可靠性和性能,其实在网上随便一搜都会有海量的文章说这个话题,大多数写得很细写得非常好,但发现很少有概览性的 ...

  10. Web前端基础(10):JavaScript(四)

    1. 伪数组arguments arguments代表的是实参.有个讲究的地方是:arguments只在函数中使用. 1.1 返回参数个数 返回函数实参的个数:arguments.length 例子: ...