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. PatentTips - Improving security in a virtual machine host

    BACKGROUND Computer viruses are a common problem for computer users. One typical mode of attack is t ...

  2. 程序猿的量化交易之路(14)--Cointrader数据表(2)

    Cointrader表结构 转载须注明出处:http://blog.csdn.net/minimicall?viewmode=contents,http://cloudtrader.top 设置(se ...

  3. ubuntu16 升级后找不到 eth0 网卡 的解决方法

    ubuntu16 升级后找不到 eth0 网卡 的解决方法 今天在VPS上一时手痒,执行了升级命令 apt-get update 更新软件包索引,源 apt-get upgrade 更新软件包 apt ...

  4. theme- 工作原理

    首先看一下theme中的设置,代码如下 <?xml version="1.0" encoding="utf-8"?> <resources&g ...

  5. 76.CGI编码

    CGI编码 "%D6%DC%C8%F0%B8%A3"; 转换到字符串中: //CGI编码转到char类型的tmpstr中中 char* change(char *str) { // ...

  6. 数据库中解析XML

    简介:OPENXML方法使用一例实现导入功能 DECLARE @strProjGUID AS VARCHAR(50)  DECLARE @strProjCode AS VARCHAR(50)  DEC ...

  7. Elasticsearch之插件扩展

    Elasticsearch之插件介绍及安装 Elasticsearch之head插件安装之后的浏览详解 Elasticsearch之kopf插件安装之后的浏览详解 Elasticsearch-2.4. ...

  8. 命令行SVN的使用

    1.检出svn  co  http://路径(目录或文件的全路径) [本地目录全路径]  --username 用户名 --password 密码svn  co  svn://路径(目录或文件的全路径 ...

  9. Beginning iOS Programming

    Beginning iOS Programming 2014年 published by Wrox

  10. HTML基础第六讲---表格

    转自:https://i.cnblogs.com/posts?categoryid=1121494 上一讲,讲了关于<控制表格及其表项的对齐方式>,在这里我要讲讲表格及其属性 ,然后大家在 ...