CodeForces - 981D Bookshelves
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
10 4
9 14 28 1 7 13 15 29 2 31
24
7 3
3 14 15 92 65 35 89
64
Note
In the first example you can split the books as follows:
In the second example you can split the books as follows:
#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的更多相关文章
- Codeforces 981D Bookshelves(按位贪心+二维DP)
题目链接:http://codeforces.com/contest/981/problem/D 题目大意:给你n本书以及每本书的价值,现在让你把n本书放到k个书架上(只有连续的几本书可以放到一个书架 ...
- Codeforces Avito Code Challenge 2018 D. Bookshelves
Codeforces Avito Code Challenge 2018 D. Bookshelves 题目连接: http://codeforces.com/contest/981/problem/ ...
- Codeforces 981 D.Bookshelves(数位DP)
Codeforces 981 D.Bookshelves 题目大意: 给n个数,将这n个数分为k段,(n,k<=50)分别对每一段求和,再将每个求和的结果做与运算(&).求最终结果的最大 ...
- 【CF981D】Bookshelves(贪心,动态规划)
[CF981D]Bookshelves(贪心,动态规划) 题面 洛谷 Codeforces 给定一个长度为\(n\)的数列,把他们划分成\(k\)段,使得每段的和的结构按位与起来最大. 题解 从高位往 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
随机推荐
- 这次OpenSSL HeartBleed漏洞是怎么一回事呢?
“心脏出血”(Heartbleed)被称为互联网史上最严重的安全漏洞之一,波及了大量常用网站.服务,包括很多人每天都在用的 Gmail 等等,可能导致用户的密码.信用卡轻易泄露.但是我们可能对它还不是 ...
- Codeforces 931.D Peculiar apple-tree
D. Peculiar apple-tree time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Round #506 (Div. 3) 题解
Codeforces Round #506 (Div. 3) 题目总链接:https://codeforces.com/contest/1029 A. Many Equal Substrings 题意 ...
- Codeforces Round #525 (Div. 2)A. Ehab and another construction problem
A. Ehab and another construction problem 题目链接:https://codeforc.es/contest/1088/problem/A 题意: 给出一个x,找 ...
- bzoj 2756 [SCOI2012]奇怪的游戏 二分+网络流
2756:[SCOI2012]奇怪的游戏 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 4926 Solved: 1362[Submit][Stat ...
- ppk和pem证书互转
首先你得去下载个putty pem:通用证书格式 ppk:为putty下面的专有格式 pem->ppk 直接通过putty下的puttygen.exe 选的Load private ke ...
- Spring学习--切面优先级及重用切点表达式
指定切面的优先级: 在同一个链接点上应用不止一个切面时 , 除非明确指定 , 否则它们的优先级是不确定的. 切面的优先级可以通过实现 Ordered 接口或利用 @Order 注解指定. 实现 Ord ...
- Android百度地图的使用
做关于位置或者定位的app的时候免不了使用地图功能,本人最近由于项目的需求需要使用百度地图的一些功能,所以这几天研究了一下,现写一下blog记录一下,欢迎大家评论指正! 一.申请AK(API Key) ...
- centos yum 安装 mysql
centos7下使用yum安装mysql 时间:2015-03-07 21:26:20 阅读:87445 评论:0 收藏:1 [点我收藏+] 标签: Cen ...
- 汕头市队赛 SRM 06 B 起伏的排名
B 起伏的排名 SRM 06 背景&&描述 天才麻将少女KPM立志要在日麻界闯出一番名堂. 在上个星期她打了n场麻将,每场麻将都有n名玩家.KPM自然记得自己的n次排名. ...