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. vue 实现tab切换动态加载不同的组件

    vue 实现tab切换动态加载不同的组件 使用vue中的is特性来加载不同的组件.具体看如下代码:这个功能对于vue比较复杂的页面可以使用上,可以把一个页面的功能拆分出来,使代码更简单.使用方式具体看 ...

  2. docker容器启动设置固定IP

    docker安装以后的网络类型 [root@insure updev]# docker network ls NETWORK ID NAME DRIVER SCOPE 14da40175b01 bri ...

  3. AI 数值计算

    数值计算,通过迭代来更新解的估计值. 1.上溢和下溢 实数按照一定的精度存储在计算机中,通常存在误差,进而可能导致一些错误. 1)下溢(underflow),例如接近0的数 2)上溢(overflow ...

  4. I2C总线通信

    UART 属于异步通信,比如电脑发送给单片机,电脑只负责把数据通过TXD 发送出来即可,接收数据是单片机自己的事情.而 I2C 属于同步通信, SCL 时钟线负责收发双方的时钟节拍, SDA 数据线负 ...

  5. qt 程序中执行额外程序和脚本

    1.最简单的,我们可以通过system直接启动一个应用程序或者脚本:(但是要调用 #include <stdlib.h>) system("./helloworld") ...

  6. Luogu P1962 斐波那契数列(矩阵乘法模板)

    传送门(其实就是求斐波那契数列....) 累了 明天再解释 做这道题需要一些关于矩阵乘法的基础知识. 1. 矩阵乘法的基础运算 只有当矩阵A的列数等于矩阵B的行数时,A与B可以相乘(A的行数不一定等于 ...

  7. Saltstack学习之二:target与模块方法的运行

    对象的管理 saltstack系统中我们的管理对象叫做target,在master上我们可以采用不同的target去管理不同的minion,这些target都是通过去管理和匹配minion的id来做的 ...

  8. 关于linux系统如何实现fork的研究(一)

    引言     fork函数是用于在linux系统中创建进程所使用,而最近看了看一个fork()调用是怎么从应用到glibc,最后到内核中实现的,这片文章就聊聊最近对这方面研究的收获吧.我们主要聊聊从g ...

  9. SkylineGlobe 6.6 版本API更新

    TEPro6.6API更新 概述 API6.6 较6.5只做了微小的更新,您可以很容易的将6.5的应用程序移植为6.6版本的应用程序. C#环境中,修改步骤如下: 创建SGWorld66实例代替SGW ...

  10. LOJ2541 PKUWC2018 猎人杀 期望、容斥、生成函数、分治

    传送门 首先,每一次有一个猎人死亡之后\(\sum w\)会变化,计算起来很麻烦,所以考虑在某一个猎人死亡之后给其打上标记,仍然计算他的\(w\),只是如果打中了一个打上了标记的人就重新选择.这样对应 ...