当前位置: 首页 > news >正文

B - Polycarp‘s Practice

Polycarp is practicing his problem solving skill. He has a list of nn problems with difficulties a_1, a_2, \dots, a_na1​,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 \max\limits_{l \le i \le r}a_il≤i≤rmax​ai​). 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 nn problems 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 \le k \le n \le 20001≤k≤n≤2000) — the number of problems and the number of days, respectively.

The second line of the input contains nn integers a_1, a_2, \dots, a_na1​,a2​,…,an​ (1 \le a_i \le 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 t_1, t_2, \dots, t_kt1​,t2​,…,tk​ (t_1 + t_2 + \dots + t_kt1​+t2​+⋯+tk​ must equal nn), where t_jtj​ 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.

Sample 1

InputcopyOutputcopy
8 3
5 4 2 6 5 1 9 2
20
3 2 3

Sample 2

InputcopyOutputcopy
5 1
1 1 1 1 1
1
5

Sample 3

InputcopyOutputcopy
4 2
1 2000 2000 2
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段,最大化每段最大值的和

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<string.h>
#include<climits>
#include<map>
#include<algorithm>
typedef long long ll;
using namespace std;
const int N = 2010;
int n, k;struct node {int num, val;
}mm[N];bool cmp1(node n1, node n2)
{return n1.val > n2.val;
}bool cmp2(node n1, node n2)
{return n1.num < n2.num;
}int main()
{cin >> n >> k;for (int i = 1; i <= n; i++){int num;cin >> num;mm[i].num = i;mm[i].val = num;}sort(mm + 1, mm + n + 1, cmp1);//先对整个序列按照数值的大小进行排序,去前k个数的和int sum = 0;for (int i = 1; i <= k; i++) sum += mm[i].val;sort(mm + 1, mm + k + 1, cmp2); //在对前k个数进行排序,做差分组cout << sum << endl;for (int i = 1; i < k; i++) cout << mm[i].num - mm[i - 1]. num << " ";cout << n - mm[k - 1].num << endl;return 0;
}

http://www.lryc.cn/news/160881.html

相关文章:

  • 朴素贝叶斯数据分类------
  • flask中的操作数据库的插件Flask-SQLAlchemy
  • arrow的使用
  • 【24种设计模式】装饰器模式(Decorator Pattern(Wrapper))
  • 小程序v-for与key值使用
  • Qt包含文件不存在问题解决 QNetworkAccessManager
  • 【视频图像篇】FastStone Capture屏幕长截图软件
  • 【C语言】每日一题(杨氏矩阵查找数)
  • 探究SpringWeb对于请求的处理过程
  • 如何使用Google Compute Engine入门指南快速创建和配置您的云虚拟机实例
  • springMVC中全局异常处理
  • 【Nginx24】Nginx学习:压缩模块Gzip
  • 我的私人笔记(zookeeper分布式安装)
  • 小程序排名优化全攻略
  • MySQL MHA
  • Java API速记手册(持续更新ing...)
  • FANUC机器人电气控制柜内部硬件电路和模块详细介绍
  • LGFormer:LOCAL TO GLOBAL TRANSFORMER FOR VIDEO BASED 3D HUMAN POSE ESTIMATION
  • 数据结构零基础入门篇(C语言实现)
  • Hugging News #0904: 登陆 AWS Marketplace
  • Redis Redis的数据结构 - 通用命令 - String类型命令 - Hash类型命令
  • vue中的几种name属性
  • 论文《面向大规模日志数据分析的自动化日志解析》翻译
  • element-ui dialog弹窗 设置点击空白处不关闭
  • 第16节-PhotoShop基础课程-修复工具组-去水印
  • conda的使用教程
  • 客户端发现pod并与之通信
  • Powershell模拟实现Linux下的tree命令
  • 嵌入式基础-电路
  • 【JS面试题】如何通过闭包漏洞在外部修改函数中的变量