【Codeforces Round 334 (Div 2)B】【贪心】More Cowbell n个物品装进m个箱子,每个箱子最多放2个物品 要求箱子空间尽可能小

时间:2022-02-08 00:21:16
B. More Cowbell
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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.

Sample test(s)
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}.


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=1e5+10,M=0,Z=1e9+7,ms63=1061109567;
int n,m;
int a[N];
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<=n;++i)scanf("%d",&a[i]);
sort(a+1,a+n+1);
int single=min(n,m*2-n);
int pair=n-single;
int ans=a[n];
for(int h=1,t=pair;h<t;++h,--t)gmax(ans,a[h]+a[t]);
printf("%d\n",ans);
}
return 0;
}
/*
【trick&&吐槽】
很多题目,处理起来很自然,相应巧妙地对应,奇偶性也就很可能是对的。

【题意】
我们有n(1e5)个物品,我们要把它们装在m(2*m>=n)个箱子里。每个箱子中只能装1~2个物品。
我们想要问你,箱子尺寸的最小值是多少。

【类型】
贪心

【分析】
首先,箱子数肯定保证了,所有物品都是能被装得下的。
然后,有一部分箱子装一个物品,有一部分箱子装两个物品。
显然, 装一个物品的箱子要放最大的物品;装两个物品的箱子要相应放当前最大和最小的。
按照这个原则贪心,更新答案即可。

唯一需要动点脑筋的问题是——
有多少个箱子装一个物品,有多少个箱子装两个物品呢?
我们不妨让所有箱子都装两个物品,然后这样物品数量可能是不够的。
即:我们m个箱子,本来最多能装2m个物品,现在却只有n个,有2m-n个空空下来了。
那么我们就可以有min(n,2m-n)个箱子,只装一个物品。

于是答案就是max(pairsum,biggest one),这样就AC啦!

*/