More Cowbell

CodeForces - 604B

Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter, where there is actually grass instead of corn. Before moving, he must pack his cowbells into k boxes of a fixed size. In order to keep his collection safe during transportation, he won't place more than two cowbells into a single box. Since Kevin wishes to minimize expenses, he is curious about the smallest size box he can use to pack his entire collection.

Kevin is a meticulous cowbell collector and knows that the size of his i-th (1 ≤ i ≤ n) cowbell is an integer si. In fact, he keeps his cowbells sorted by size, so si - 1 ≤ si for any i > 1. Also an expert packer, Kevin can fit one or two cowbells into a box of size s if and only if the sum of their sizes does not exceed s. Given this information, help Kevin determine the smallest s for which it is possible to put all of his cowbells into k boxes of size s.

Input

The first line of the input contains two space-separated integers n and k (1 ≤ n ≤ 2·k ≤ 100 000), denoting the number of cowbells and the number of boxes, respectively.

The next line contains n space-separated integers s1, s2, ..., sn (1 ≤ s1 ≤ s2 ≤ ... ≤ sn ≤ 1 000 000), the sizes of Kevin's cowbells. It is guaranteed that the sizes si are given in non-decreasing order.

Output

Print a single integer, the smallest s for which it is possible for Kevin to put all of his cowbells into k boxes of size s.

Examples

Input
2 1
2 5
Output
7
Input
4 3
2 3 5 9
Output
9
Input
3 2
3 5 7
Output
8

Note

In the first sample, Kevin must pack his two cowbells into the same box.

In the second sample, Kevin can pack together the following sets of cowbells: {2, 3}, {5} and {9}.

In the third sample, the optimal solution is {3, 5} and {7}.

sol:很显然答案是可以二分的,难点在于判断当前答案是否可行,一种较为容易想到的贪心,尽量用一个最大的配上一个最小的,易知一定是最优的

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,m,a[N];
inline bool Judge(int mid)
{
int l=,r=n,cnt=;
while(l<=r)
{
if(a[l]+a[r]<=mid)
{
cnt++; l++; r--;
}
else
{
cnt++; r--;
}
}
return (cnt<=m)?:;
}
int main()
{
int i;
R(n); R(m);
for(i=;i<=n;i++) R(a[i]);
sort(a+,a+n+);
int l=a[n],r=;
while(l<=r)
{
int mid=(l+r)>>;
if(Judge(mid)) r=mid-;
else l=mid+;
}
Wl(l);
return ;
}
/*
input
2 1
2 5
output
7 input
4 3
2 3 5 9
output
9 input
3 2
3 5 7
output
8
*/

codeforces604B的更多相关文章

随机推荐

  1. mysql 数据表 增删改查

    用户操作: mysql -u root -p 登录root用户: SHOW DATABASES; 显示所有的数据库名称: USE linuxcast; 切入linuxcast数据库: CREATE T ...

  2. Android学习之基础知识七—碎片的最佳实践

    一.Android碎片(Fragment)的最佳实践——简易版新闻应用 第一步:新建FragmentBestPractice项目,在app/build.gradle当中添加:RecyclerView ...

  3. Android学习之基础知识四-Activity活动6讲(体验Activity的生命周期)

    一.体验活动的生命周期的执行 代码组成: 1.三个Java类:MainActivity.java.NormalActivity.java.DialogActivity.java 2.三个布局文件:ac ...

  4. docker知识复习

    1.镜像基于内容寻址 基于内容寻址的实现,使用了两个目录:/var/lib/docker/image和/var/lib/docker/overlay, 后面的这个根据存储驱动的名称不同,而目录名不同. ...

  5. 解决PowerDesigner 16 Generate Datebase For Sql2005/2008 对象名sysproperties无效的问题

    在PowerDesigner 16 中生成的sql语句,在执行的时候报错:对象名sysproperties 无效的错误;造成此问题的原因是由于Sql 2005.2008 删除了系统表 sysprope ...

  6. IDC Digital Transition Annual Festival(2018.10.19)

    时间:2018.10.19地点:北京万达文化酒店

  7. [Oracle][PDB]PDB restore/recover 过程记录

    友人给的PDB restore / recover 的过程. 实际上会创建一个辅助Instance,然后抽取出PDB,进行恢复. (10:31:59) frank.yan: [Administrato ...

  8. 记一次MongoDB裸奔

    导言 大意失荆州,裸奔的 MongoDB 被黑了.虽然并不是什么非常重要的数据,但也给自己敲响的一个警钟.虽然我们平时不容易接触到数据安全,但我们在开发,部署项目的时候,一定要养成良好的安全意识. 根 ...

  9. hexo——轻量、简易、高逼格的博客

    背景 写blog虽然经历了N多不同时代的产品,恒久不变的始终是自己无人问津的网站.虽然没几个人看,还是隔断时间就要折腾一下.从最开始的wordpress,到tale,到现在的hexo,网站变得越来越简 ...

  10. Linux Namespace : IPC

    IPC namespace 用来隔离 System V IPC 对象和 POSIX message queues.其中 System V IPC 对象包含共享内存.信号量和消息队列,笔者在<Sy ...