time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

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}.

【题目链接】:http://codeforces.com/contest/604/problem/B

【题解】



二分最后的箱子容量;

左端点应该是最大的cowbell,右端点无限大.

看看m需要用几个箱子d;

(如果加上这个数大于m或装了两个就不装了);

(装的时候,先装大的,大的尝试和当前剩余最小的组合在一起,如果能组合就组合,不能的话大的单独装.);

if (d <= k)

ans = m,r = m-1;

else

if (d > k)//箱子用多了那就增大箱子容量

l = m+1;

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int MAXN = 1e5+100;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0); int n,k;
int s[MAXN]; int ok(int lim)
{
int tot = 0;
int r = n,l = 1;
while (l <= r)
{
if (l!=r && s[r]+s[l]<=lim)
{
r--,l++;
tot++;
}
else
tot++,r--;
}
return tot;
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);rei(k);
int l = 1,r = 21e8;
rep1(i,1,n)
{
rei(s[i]);
l = max(s[i],l);
}
int ans = -1;
while (l <= r)
{
int m = (l+r)>>1;
if (ok(m)<=k)
{
ans = m;
r = m-1;
}
else
l = m+1;
}
cout << ans << endl;
return 0;
}

【31.72%】【codeforces 604B】More Cowbell的更多相关文章

  1. 【CodeForces 604B】F - 一般水的题1-More Cowbe

    Description Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter ...

  2. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  3. 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  4. 【codeforces 785D】Anton and School - 2

    [题目链接]:http://codeforces.com/contest/785/problem/D [题意] 给你一个长度为n的括号序列; 让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后 ...

  5. 【30.23%】【codeforces 552C】Vanya and Scales

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【codeforces 754D】Fedor and coupons

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【codeforces 760A】Petr and a calendar

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【codeforces 755D】PolandBall and Polygon

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【21.37%】【codeforces 579D】"Or" Game

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. eclipse- DDMS截图功能使用

    如何使用eclipse的截图功能呢 1.打开eclipse 2.连接手机 3.打开eclipse的DDMS插件. 4.选中手机 5.点击上面的摄像机图标,就可以截图了 如果你打开了DDMS以后,没有发 ...

  2. golang 建临时文件目录以及删除

    package main import ( "fmt" "os" "path/filepath" "strings" ) ...

  3. Python Unittest模块测试执行

    记录一下Unittest的测试执行相关的点 一.测试用例执行的几种方式 1.通过unittest.main()来执行测试用例的方式: if __name__ == "__main__&quo ...

  4. 关于MyBatis sqlSession的一点整理

    工作中,需要学习一下MyBatis sqlSession的产生过程,翻看了mybatis-spring的源码,阅读了一些mybatis的相关doc,对mybatis sqlSession有了一些认知和 ...

  5. linux下加入用户并赋予root权限

    1.加入用户.首先用adduser命令加入一个普通用户,命令例如以下: #adduser tommy //加入一个名为tommy的用户 #passwd tommy   //改动password Cha ...

  6. [Vue + TS] Create Type-Safe Vue Directives in TypeScript

    Directives allow us to apply DOM manipulations as side effects. We’ll show you how you can create yo ...

  7. setting-在设置中添加新的选项

    如下图的“通知栏调出方式” 具体实现如下 1.在 res/xml/settings_headers.xml 文件中添加如下内容 <preference-headers xmlns:android ...

  8. Css 显示删除条目效果

    样式设置

  9. Mongodb总结5-通过装饰模式,用Mongodb解决Hbase的不稳定问题

    最近继续学习Mongodb的根本原因,是为了解决今天的问题.项目中用到了Hbase,生产环境服务器用了3台,但是不够稳定,每2天左右,就连不上了.重启就好了,当然,这是一个历史遗留问题.我在想,是不是 ...

  10. HTTP--Request Headers及Cookies

    简介: HTTP客户程序(例如浏览器),向服务器发送请求的时候必须指明请求类型(一般是GET或者POST).如有必要,客户程序还可以选择发送其他的请求头.大多数请求头并不是必需的,但Content-L ...