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. 如何使用Navicat恢复数据库脚本

    Navicat 可以做数据库备份,当然也可以做数据库脚本恢复了.操作很简单. 1.连接需要恢复的数据库.鼠标右键点击,选择[运行SQL文件] 2.在弹出的窗口中选择sql文件,继续下一步即可. 余不赘 ...

  2. rest与restful

      知乎上面摘抄的,感觉不错,分享下:  https://www.zhihu.com/question/28557115 1. REST描述的是在网络中client和server的一种交互形式:RES ...

  3. libusb 示例

    #include <usb.h> #include <stdio.h> #define VERSION "0.1.0" #define VENDOR_ID ...

  4. 移动端浏览器touch事件的研究总结

    $("body").on("touchstart", function(e) {     e.preventDefault();     startX = e. ...

  5. 【BZOJ2301】【HAOI2011】Problem b [莫比乌斯反演]

    Problem b Time Limit: 50 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description 对于给出的n个询问,每次 ...

  6. 51nod 1020 逆序排列——dp

    在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数. 如2 4 3 1中,2 1,4 3,4 1,3 1是逆序 ...

  7. noip2016 普及组

    T1 买铅笔 题目传送门 #include<cstdio> #include<cstring> #include<algorithm> using namespac ...

  8. mongoDB的简单使用

    1.客户端连接: ./mongo 2.数据库 一个mongodb中可以建立多个数据库. MongoDB的默认数据库为"db",该数据库存储在data目录中. MongoDB的单个实 ...

  9. MFC/Socket网络编程

    转载: https://jingyan.baidu.com/article/676629974557c254d51b84da.html

  10. linux知识复习1-dup dup2

    #include <sys/stat.h> #include <string.h> #include <fcntl.h> #include <stdio.h& ...