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. MongoDB可视化工具--Robo 3T 使用教程

    MongoDB可视化工具--Robo 3T 使用教程 1. 到官网下载Robo 3T,网址如: https://robomongo.org/download. 2. 下载安装成功后,打开后点击左上角的 ...

  2. C++ 函数模板&类模板

    函数模板 #include <iostream> #include <string> using namespace std; template <typename T& ...

  3. 如何计算PCB设计中的阻抗

    关于阻抗的话题已经说了这么多,想必大家对于阻抗控制在pcb layout中的重要性已经有了一定的了解.俗话说的好,工欲善其事,必先利其器.要想板子利索的跑起来,传输线的阻抗计算肯定不能等闲而视之. 在 ...

  4. Unexpected error from external database driver (1)

    当尝试把Excel导入SQL时,发生此异常: Unexpected error from external database driver (1). 在网上查找到一个解决方法,网址http://dat ...

  5. 判断response.data是否为空

    需要对response.data进行判断,是否有数据返回.如果是空的,将要处理一些事情,反之,又要处理另外一些事情. 在jQuery程序中,有一个方法:$.isEmptyObject().此方法在an ...

  6. 11.10 (下午)开课二个月零六天(ajax验证用户名,ajax调数据库)

    用ajax验证用户名是否可用 testuid.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&quo ...

  7. Python 学习 第九篇:模块

    模块是把程序代码和数据封装的Python文件,也就是说,每一个以扩展名py结尾的Python源代码文件都是一个模块.每一个模块文件就是一个独立的命名空间,用于封装顶层变量名:在一个模块文件的顶层定义的 ...

  8. [开源 .NET 跨平台 Crawler 数据采集 爬虫框架: DotnetSpider] [四] JSON数据解析

    [DotnetSpider 系列目录] 一.初衷与架构设计 二.基本使用 三.配置式爬虫 四.JSON数据解析与配置系统 五.如何做全站采集 场景模拟 接上一篇, JD SKU对应的店铺信息是异步加载 ...

  9. Jq_DOM元素方法跟JQuery 核心函数跟JQuery 事件方法

    JQuery DOM 元素 函数                            描述 .get()                           从队列中删除所有未运行的项目. .ind ...

  10. Linux下分布式系统以及CAP理论分析

    CAP理论被很多人拿来作为分布式系统设计的金律,然而感觉大家对CAP这三个属性的认识却存在不少误区,那么什么是CAP理论呢?CAP原本是一个猜想,2000年PODC大会的时候大牛Brewer提出的,他 ...