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. PAT A1129 Recommendation System (25 分)——set,结构体重载小于号

    Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...

  2. 栈(stack)信息

    栈在JVM虚拟机中是线程的一块私有空间,比如存储函数的调用信息.局部变量等 特性 先进后出和后进先出即FIFO 借用网络的一个图,感觉看完就可以了解了 最先调用的函数压入栈低,最后压入得函数在栈顶,函 ...

  3. ASP.NET 文件操作类

    1.读取文件 2.写入文件 using System; using System.Collections.Generic; using System.IO; using System.Linq; us ...

  4. node-inspector调试工具

    1.  npm或者cnpm 安装node-inspector 命令:  cnpm install -g node-inspector     (说明: 全局安装) 2.  使用node-inspect ...

  5. 最近找工作,有招JAVA开发的可以联系我,如果不嫌弃我2年前用C,也可以联系我

    java涉及到的技术工具:HSF.Pandora.Notify.Metaq.Diamond.Tddl.ScheduleX.精卫.Switch.BCP.Tair.Hbase.Mysql.Ads.Tlog ...

  6. Luogu2792 JSOI2008 小店购物 最小树形图

    传送门 被题意杀 本以为一个种类的物品一定要一起买 看了题解才知道可以先把所有要买的物品买一个,剩下要买的物品就可以得到这个种类的物品能够得到的最大优惠-- 所以现在只需要知道:第一次买所有物品一遍时 ...

  7. ASP.NET Web API上实现 Web Socket - 转

    1. 什么是Web Socket Web Socket是Html5中引入的通信机制,它为浏览器与后台服务器之间提供了基于TCP的全双工的通信通道.用以替代以往的LongPooling等comet st ...

  8. js 稍微判断下浏览器 pc 还是手机

        function isMobile() {    var a=navigator.userAgent;   var ref=/.*(Android|iPhone|SymbianOS|iPad| ...

  9. Rabbitmq-topic演示

    在direct演示里,我们的日志系统实现了可选择性的接收日志.但仍旧有一些限制:不能基于多种标准进路由.在一个完整的日志系统中,我们可能不仅要根据日志的严重级别来接收日志,可能需要基于日志的来源来进行 ...

  10. STM32串口打印输出乱码的解决办法

    前言 最近在试用uFUN开发板,下载配套的Demo程序,串口数据输出正常,当使用另一个模板工程,调用串口printf调试功能时,输出的却是乱码,最后发现是外部晶振频率不一样.很多STM32开发板都是使 ...