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.

Codeforces (c) Copyright 2010-2018 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jul/18/2018 00:42:04UTC+8 (d2).

Desktop version, switch to mobile version.

Privacy Policy

题解:就是找前K大的几个数,输出值几个大数之间的距离;找到前K大的数的位置,然后往两侧扩展即可。

AC代码为:

#include<bits/stdc++.h>

using namespace std;

int a[2010],vis[2010],ans[2010];

struct Node{

    int num,id;

} node[2010];

bool cmp(Node a,Node b)

{

    return a.num>b.num;

}

bool cmp1(Node a, Node b)

{

    return a.id<b.id;

}

int main()

{

    ios::sync_with_stdio(false);

    cin.tie(0);

    int n,k;

    cin>>n>>k;

    memset(vis,0,sizeof vis);

    for(int i=1;i<=n;i++)

    {

        cin>>a[i];

        node[i].num=a[i];

        node[i].id=i;

    }

    sort(node+1,node+1+n,cmp);

    int sum=0;

    for(int i=1;i<=k;i++) sum+=node[i].num,vis[node[i].id]=1;

    cout<<sum<<endl;

    int temp=0,flag1,flag2;

    for(int i=1;i<=n;i++)

    {

        if(vis[i])

        {

            flag1=i-1;flag2=i+1;

            while(!vis[flag1] && flag1>0&&flag1<=n) vis[flag1]=1,flag1--;

            while(!vis[flag2] && flag2>0&&flag2<=n) vis[flag2]=1,flag2++;

            ans[temp++]=flag2-flag1-1; 

            i=flag2-1;

        }

    }

    for(int i=0;i<temp;i++) i==temp-1 ? cout<<ans[i]<<endl : cout<<ans[i]<<" ";

    return 0;

}

CodeForces-1006B-Polycarp's Practice的更多相关文章

  1. CF 1006B Polycarp's Practice【贪心】

    Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a1,a ...

  2. Codeforces 659F Polycarp and Hay 并查集

    链接 Codeforces 659F Polycarp and Hay 题意 一个矩阵,减小一些数字的大小使得构成一个连通块的和恰好等于k,要求连通块中至少保持一个不变 思路 将数值从小到大排序,按顺 ...

  3. Codeforces 723C. Polycarp at the Radio 模拟

    C. Polycarp at the Radio time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...

  4. codeforces 727F. Polycarp's problems

    题目链接:http://codeforces.com/contest/727/problem/F 题目大意:有n个问题,每个问题有一个价值ai,一开始的心情值为q,每当读到一个问题时,心情值将会加上该 ...

  5. codeforces 723C : Polycarp at the Radio

    Description Polycarp is a music editor at the radio station. He received a playlist for tomorrow, th ...

  6. [Codeforces 864B]Polycarp and Letters

    Description Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string s con ...

  7. Codeforces 861D - Polycarp's phone book

    861D - Polycarp's phone book 思路:用map做的话,只能出现一次循环,否则会超时. 代码: #include<bits/stdc++.h> using name ...

  8. CodeForces 81D.Polycarp's Picture Gallery 乱搞

    D. Polycarp's Picture Gallery time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  9. 『ACM C++』 Codeforces | 1005D - Polycarp and Div 3

    今天佛了,魔鬼周一,在线教学,有点小累,但还好,今天AC了一道,每日一道,还好达成目标,还以为今天完不成了,最近任务越来越多,如何高效完成该好好思考一下了~最重要的还是学业的复习和预习. 今日兴趣新闻 ...

  10. Codeforces 659F Polycarp and Hay【BFS】

    有毒,自从上次选拔赛(哭哭)一个垃圾bfs写错之后,每次写bfs都要WA几发...好吧,其实也就这一次... 小白说的对,还是代码能力不足... 非常不足... 题目链接: http://codefo ...

随机推荐

  1. 『嗨威说』算法设计与分析 - PTA 程序存储问题 / 删数问题 / 最优合并问题(第四章上机实践报告)

    本文索引目录: 一.PTA实验报告题1 : 程序存储问题 1.1 实践题目 1.2 问题描述 1.3 算法描述 1.4 算法时间及空间复杂度分析 二.PTA实验报告题2 : 删数问题 2.1 实践题目 ...

  2. c/c++求最大公约数和最小公倍数

    最大公约数GCD(Greatest Common Divisor) 最常见的求两个数的最大公约数的算法是辗转相除法,也叫欧几里得算法 该算法的c++语言实现如下: #include<iostre ...

  3. 利用Python学习线性代数 -- 1.1 线性方程组

    利用Python学习线性代数 -- 1.1 线性方程组 本节实现的主要功能函数,在源码文件linear_system中,后续章节将作为基本功能调用. 线性方程 线性方程组由一个或多个线性方程组成,如 ...

  4. 将Swagger2文档导出为HTML或markdown等格式离线阅读

    网上有很多<使用swagger2构建API文档>的文章,该文档是一个在线文档,需要使用HTTP访问.但是在我们日常使用swagger接口文档的时候,有的时候需要接口文档离线访问,如将文档导 ...

  5. C++学习第二天(打卡)

    C++ new 可以很方便的 分配一段内存. 比如 int *test= new int ; int n; cin>>n; int * test =new int [n]; 可以实现动态分 ...

  6. springboot+logback日志输出企业实践(下)

    目录 1.引言 2. 输出 logback 状态数据 3. logback 异步输出日志 3.1 异步输出配置 3.2 异步输出原理 4. springboot 多环境下 logback 配置 5. ...

  7. day48天jQuary

    今日内容 jQuery jQuery引入 下载链接:[jQuery官网](https://jquery.com/),首先需要下载这个jQuery的文件,然后在HTML文件中引入这个文件,就可以使用这个 ...

  8. Redis的面试问题总结,面试跳槽必备

    1.什么是redis? Redis 是一个基于内存的高性能key-value数据库. 2.Reids的特点 Redis本质上是一个Key-Value类型的内存数据库,很像memcached,整个数据库 ...

  9. Selenium+Java(三)Selenium元素定位

    前言 使用Selenium做元素定位的时候,需要用到HTML的知识,所以最好是能懂得HTML的基本知识. 一.页面元素的查看(以百度为例) 打开IE浏览器,点击F12进入开发者模式,点击图中红圈圈中的 ...

  10. Java语法进阶10-多线程

    多线程 并发与并行.进程,线程调度自行百度 线程(thread):是一个进程中的其中一条执行路径,CPU调度的最基本调度的单位.同一个进程中线程可以共享一些内存(堆.方法区),每一个线程又有自己的独立 ...