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的概 ...
随机推荐
- malloc calloc realloc
三个函数的申明分别是: void* realloc(void* ptr, unsigned newsize); void* malloc(unsigned size); void* calloc(si ...
- photo@PKU
- org.apache.hadoop.hdfs.server.datanode.DataNode: Exception in receiveBlock for block
Hbase依赖的datanode日志中如果出现如下报错信息:DataXceiverjava.io.EOFException: INFO org.apache.hadoop.hdfs.server.da ...
- ActiveMQ(2) ActiveMQ创建HelloWorld
启动ActiveMQ: 请参见:ActiveMQ(1) 初识ActiveMQ 创建Maven工程: pom文件: <project xmlns="http://maven.apache ...
- Spring 学习笔记(二)
一.Spring 中的bean配置 –配置形式:基于 XML 文件的方式:基于注解的方式 –Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & 实例工厂方法).Fac ...
- HDU2066一个人的旅行---(多起点多终点最短路径)
http://acm.hdu.edu.cn/showproblem.php?pid=2066 一个人的旅行 Time Limit: 1000/1000 MS (Java/Others) Memo ...
- 【洛谷 P3846】 [TJOI2007]可爱的质数 (BSGS)
题目链接 \(BSGS\)模板题..不会点这里 #include <cstdio> #include <cmath> #include <map> using na ...
- DotNet 学习笔记 MVC模型
Model Binding Below is a list of model binding attributes: •[BindRequired]: This attribute adds a mo ...
- input button 不能在后台用Enabled
<input type="button" value="上传" class="uploadButton" runat="s ...
- PL/SQL 05 存储过程 procedure
--存储过程(不带参数) create or replace procedure 存储过程名as 变量.常量声明;begin 代码;end; --存储过程(带输入参数) create or rep ...