0
点赞
收藏
分享

微信扫一扫

D. Bookshelves(贪心+dp)

东林梁 2022-08-10 阅读 73

D. Bookshelves

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Mr Keks is a typical white-collar in Byteland.


He has a bookshelf in his office with some books on it, each book has an integer positive price.


Mr Keks defines the value of a shelf as the sum of books prices on it.


Miraculously, Mr Keks was promoted and now he is moving into a new office.


He learned that in the new office he will have not a single bookshelf, but exactly kk bookshelves. He decided that the beauty of the kk shelves is the bitwise AND of the values of all the shelves.


He also decided that he won't spend time on reordering the books, so he will place several first books on the first shelf, several next books on the next shelf and so on. Of course, he will place at least one book on each shelf. This way he will put all his books on kk shelves in such a way that the beauty of the shelves is as large as possible. Compute this maximum possible beauty.


Input

The first line contains two integers nn and kk (1≤k≤n≤501≤k≤n≤50) — the number of books and the number of shelves in the new office.


The second line contains nn integers a1,a2,…ana1,a2,…an, (0<ai<2500<ai<250) — the prices of the books in the order they stand on the old shelf.


Output

Print the maximum possible beauty of kk shelves in the new office.


Examples

input Copy

10 4
9 14 28 1 7 13 15 29 2 31

output Copy

24

input Copy

7 3
3 14 15 92 65 35 89

output Copy

64


题目大概:

给你n个数,要求你分成k堆。每堆的内部加和,每堆之间是相与。问最大的值。

思路:

既然是相与,那么最大的情况一定是所有的2进制数都是1的情况。那么可以从最高位的2进制数开始枚举。用dp来判断这个位置是否可以是1。这样贪心的做,一定是最大的。

dp【i】【j】表示,用了前j个数放了i堆,在满足前面已经放过1的条件下,本位是否可以放1.

代码:

#include <bits/stdc++.h>

using namespace std;
const int maxn=55;
long long a[maxn];
long long sum[maxn];
long long dp[maxn][maxn];
int main()
{
int n,k1;
scanf("%d%d",&n,&k1);
for(int i=1;i<=n;i++)
{
scanf("%I64d",&a[i]);
}
for(int i=1;i<=n;i++)
{
sum[i]=sum[i-1]+a[i];
}
long long tem=0;
for(int bit=55;bit>=0;bit--)
{
long long ans=((long long)1<<bit);
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int k=1;k<=k1;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=0;j<i;j++)
{
if(dp[k-1][j]&&((sum[i]-sum[j])&ans)&&(((sum[i]-sum[j])&tem)==tem))
{
dp[k][i]=1;
}
}
}
}
if(dp[k1][n])
{
tem+=ans;
}
}
printf("%I64d\n",tem);
return 0;
}




举报

相关推荐

0 条评论