「CF981D」Bookshelves【贪心+dp】

本文探讨了一种算法,旨在解决将一系列正整数价格的书籍均匀分配到多个书架上,以最大化所有书架上书籍价格总和的按位与运算结果的问题。通过动态规划策略,文章详细介绍了如何从高位到低位选择最优解,确保了每一步的选择都能得到最大可能的按位与值。

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 kkk bookshelves. He decided that the beauty of the ? shelves is the bitwise ANDANDAND 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 ? 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 nnn and kkk (1≤k≤n≤50)(1≤k≤n≤50)(1kn50) — the number of books and the number of shelves in the new office.

The second line contains ? integers a1,a2,…an,(0&lt;ai&lt;250)a_1,a_2,…a_n, (0&lt;a_i&lt;250)a1,a2,an,(0<ai<250) — the prices of the books in the order they stand on the old shelf.

Output

Print the maximum possible beauty of kkk 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)&amp;(13+15)&amp;(29+2)&amp;(31)=24(9+14+28+1+7)\&amp;(13+15)\&amp;(29+2)\&amp;(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)&amp;(65)&amp;(35+89)=64(3+14+15+92)\&amp;(65)\&amp;(35+89)=64(3+14+15+92)&(65)&(35+89)=64.

  • 题意

    • 就是求把一个序列分成若干个连续子序列,使得这些子序列的元素和按位与起来最大
  • 解法

    • 看的tutorialtutorialtutorial,感觉挺有道理的233
    • 从高位到低位能选就选,那么对于给定的数如何判断能不能选呢?显然写个简单的dp就行了,定义dp[i][j]表示是否能把前i个数分成j段连续子序列使得元素和按位与起来能得到这个需要判断的数,然后注意的是后来判断的时候需要带上前面已经取到的位,不然你每次取的分段不一定都相同,导致结果可能偏大
  • 附代码:

    #include<bits/stdc++.h>
    
    using namespace std;
    typedef long long ll;
    const int maxn=55;
    
    int n,k;ll a[maxn],ans=0,sum[maxn];
    bool dp[maxn][maxn];
    
    bool check(ll cur)
    {
    	memset(dp,false,sizeof(dp));
    	dp[0][0]=true;
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=k;j++){
    			for(int l=1;l<=i;l++) {
    				if((cur&(sum[i]-sum[l-1]))==cur) dp[i][j]|=dp[l-1][j-1]; 
    			}
    		}
    	}
    	return dp[n][k];
    }
    
    int main()
    {
    	scanf("%d %d",&n,&k);
    	for(int i=1;i<=n;i++) scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i];
    	for(int i=55;i>=0;i--) if(check(ans|(1LL<<i))) ans|=(1LL<<i);
    	printf("%lld\n",ans);
    }
    
38.He took his umbrella ______ it should rain.(2分) A in case of B in case C for fear D in time 39.Not until this term ( ) to realize how important this subject is to his future career as a diplomat.(2分) A he began B did he begin C he has begun D that he has begun 40.Great changes ______ in Shanghai since the beginning of the reform and opening-up policy.(2分) A took place B has taken place C has been taken place D have taken place 41.The larger the house is, ______.(2分) A the higher rent it is B the higher rent it would have C the higher the rent is D the higher rent it would be 42.Every time Jane has trouble ______ her car started, Sean will show up to lend a hand.(2分) A get B getting C to getting D to get44.Some bookshelves have been moved out of this office to make ______ for more computers.(2分) A space B place C room D position46. Wendy: Have you been to the new bakery on the corner? Arthur: NO, how is it? Wendy: It is heaven!________! (2分) A Their cakes are to strive for B Their cakes are to pay for C Their cakes are to struggle for D Their cakes are to die for 47.Dr. Jones, many students want to see you.( ) they wait here or outside?(2分) A Do B Will C Shall D Would 48.( ) who would like to go on the trip should put their names on the list. (2分) A Those B These C Somebody D The ones 49. Kate: Do you mind opening the door for me? Bob: _________. (2分) A It’s nothing B That’s all right C Yes, I’ll do it D Not at all 50.They first stop at a board _____ the menu is displayed.(2分) A when B while C where D which
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值