Discription

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 kkbookshelves. 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
10 4
9 14 28 1 7 13 15 29 2 31
Output
24
Input
7 3
3 14 15 92 65 35 89
Output
64

Note

In the first example you can split the books as follows:

(9+14+28+1+7)&(13+15)&(29+2)&(31)=24.(9+14+28+1+7)&(13+15)&(29+2)&(31)=24.

In the second example you can split the books as follows:

(3+14+15+92)&(65)&(35+89)=64.
 
 
 
    不难想到从高位到低位贪心,根据字段和的sum的子集里是否有当前贪心的ans,来判断可以转移的点对,跑一遍类dp就行了。
 
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=65;
ll ci[maxn],sum[maxn],ans;
bool can[maxn][maxn];
int n,k; inline bool solve(){
memset(can,0,sizeof(can));
can[0][0]=1;
for(int i=1;i<=n;i++)
for(int j=0;j<i;j++) if(((sum[i]-sum[j])&ans)==ans){
for(int u=0;u<=j;u++) if(can[j][u]) can[i][u+1]=1;
} return can[n][k];
} int main(){
ci[0]=1;
for(int i=1;i<=60;i++) ci[i]=ci[i-1]+ci[i-1]; scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++) cin>>sum[i],sum[i]+=sum[i-1]; for(int i=60;i>=0;i--){
ans|=ci[i];
if(!solve()) ans^=ci[i];
} cout<<ans<<endl;
return 0;
}

  

CodeForces - 981D Bookshelves的更多相关文章

  1. Codeforces 981D Bookshelves(按位贪心+二维DP)

    题目链接:http://codeforces.com/contest/981/problem/D 题目大意:给你n本书以及每本书的价值,现在让你把n本书放到k个书架上(只有连续的几本书可以放到一个书架 ...

  2. Codeforces Avito Code Challenge 2018 D. Bookshelves

    Codeforces Avito Code Challenge 2018 D. Bookshelves 题目连接: http://codeforces.com/contest/981/problem/ ...

  3. Codeforces 981 D.Bookshelves(数位DP)

    Codeforces 981 D.Bookshelves 题目大意: 给n个数,将这n个数分为k段,(n,k<=50)分别对每一段求和,再将每个求和的结果做与运算(&).求最终结果的最大 ...

  4. 【CF981D】Bookshelves(贪心,动态规划)

    [CF981D]Bookshelves(贪心,动态规划) 题面 洛谷 Codeforces 给定一个长度为\(n\)的数列,把他们划分成\(k\)段,使得每段的和的结构按位与起来最大. 题解 从高位往 ...

  5. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  6. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  7. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  8. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  9. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

随机推荐

  1. [ST表/贪心] NOI2010 超级钢琴

    [NOI2010]超级钢琴 题目描述 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i ...

  2. poj3683 2-sat Priest John's Busiest Day

    Description John is the only priest in his town. September 1st is the John's busiest day in a year b ...

  3. Xamarin+vs2010部署错误:error MSB6004: 指定的任务可执行文件位置\sdk\\tools\zipalign.exe”无效

    好不容易配好了Xamarin和vs2010,也搞好了GenyMotion的虚拟机配置,开始调试的时候又报出了这样的错误: error MSB6004: 指定的任务可执行文件位置"C:\Use ...

  4. Hibernate 懒加载 错误----no session

    错误: unable to evaluate the expression Method threw 'org.hibernate.LazyInitializa org.hibernate.LazyI ...

  5. (转载)--SG函数和SG定理【详解】

    在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念:        P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败.        N点:必胜点 ...

  6. szoj461【四校联考0430】挑战

    传送门:(涉及版权忽略) [题解] 我们发现n的范围很小,提示我们可以折半,然后我们就会了O(T2^(n/2)*n)的做法,然而会T. 考虑如何优化.直接排序会多一个log(2^(n/2))也就是n, ...

  7. 2017年上海金马五校程序设计竞赛:Problem A : STEED Cards (STL全排列函数)

    Description Corn does not participate the STEED contest, but he is interested in the word "STEE ...

  8. BZOJ1037 DP

    2013-11-15 21:51 原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1037 拿到这道题想到了DP,后来发现三维无法确定的表示状 ...

  9. JS组件入门

    用React感觉component老好用了. 那如何用原生JS来模拟实现React中的component方法呢:http://huziketang.com/blog/posts/detail?post ...

  10. linux 链表实例应用程序【转】

    转自:http://blog.csdn.net/echo_qiang/article/details/6233057 /**************************************** ...