Codeforces Round #498 (Div. 3)-B. Polycarp's Practice

B. Polycarp's Practice

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp is practicing his problem solving skill. He has a list of nn problems with difficulties a1,a2,…,ana1,a2,…,an, respectively. His plan is to practice for exactly kk days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all nn problems in exactly kk days.

Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in kk days he will solve all the nn problems.

The profit of the jj-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the jj-th day (i.e. if he solves problems with indices from ll to rr during a day, then the profit of the day is maxl≤i≤raimaxl≤i≤rai). The total profit of his practice is the sum of the profits over all kk days of his practice.

You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all nnproblems between kk days satisfying the conditions above in such a way, that the total profit is maximum.

For example, if n=8,k=3n=8,k=3 and a=[5,4,2,6,5,1,9,2]a=[5,4,2,6,5,1,9,2], one of the possible distributions with maximum total profit is: [5,4,2],[6,5],[1,9,2][5,4,2],[6,5],[1,9,2]. Here the total profit equals 5+6+9=205+6+9=20.

Input

The first line of the input contains two integers nn and kk (1≤k≤n≤20001≤k≤n≤2000) — the number of problems and the number of days, respectively.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤20001≤ai≤2000) — difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).

Output

In the first line of the output print the maximum possible total profit.

In the second line print exactly kk positive integers t1,t2,…,tkt1,t2,…,tk (t1+t2+⋯+tkt1+t2+⋯+tk must equal nn), where tjtj means the number of problems Polycarp will solve during the jj-th day in order to achieve the maximum possible total profit of his practice.

If there are many possible answers, you may print any of them.

Examples

input

Copy

8 3
5 4 2 6 5 1 9 2

output

Copy

20
3 2 3

input

Copy

5 1
1 1 1 1 1

output

Copy

1
5

input

Copy

4 2
1 2000 2000 2

output

Copy

4000
2 2

Note

The first example is described in the problem statement.

In the second example there is only one possible distribution.

In the third example the best answer is to distribute problems in the following way: [1,2000],[2000,2][1,2000],[2000,2]. The total profit of this distribution is 2000+2000=40002000+2000=4000.

题意:给你一个数组 , 包含n个数 , 将他分成k部分 , 每部分里的都有一个最大值

可以任意划分k部分 , 但是最大的k个数是确定的。

 

思路:先从小到大排序一下 , 后k个值相加 , 把这k个值的下标标记一下 ,最后再拿上一个被标记的减去当前标记的。

当k = 1 的时候 , 直接输出n 即可。

代码:

#include <stdio.h>
#include <iostream>
#include <bits/stdc++.h>
const int maxn = 2008;
using namespace std;
 int main()
 {
 	int n , k  , a[maxn] , b[maxn] , book[maxn] , c[maxn] , t=1 , ans = 0 , ret = 0;
 	scanf("%d %d" , &n , &k);
 	memset(book , 0 , sizeof(book));
 	for(int i = 1 ; i <= n ; i++)
 	{
 		scanf("%d" , &a[i]);
 		b[i] = a[i];
	 }
	 sort(a+1 , a+n+1);
	 for(int i = n ; i > n-k ;i--)
	 {
	 	ret+=a[i];
	 	book[a[i]]++;	
	 }
	 printf("%d\n" , ret);
	
	 for(int i = 1 ; i <= n ; i++)
	 {
		 if(book[b[i]] > 0)
		 {
		 	book[b[i]]--;
		 	c[t++]=i;
		 } 
	 }
	if(k == 1)
	 {
	 	printf("%d\n" , n);
	  } 
	  else
	  {
	  	printf("%d " , c[1]);
		 for(int i = 1 ; i < t-2 ; i++)
		 {
		 	printf("%d " ,c[i+1]-c[i]);
		 }
		 printf("%d\n" , n-c[t-2]);

	  }
 	return 0;
 }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值