D. Bookshelves+Avito Code Challenge 2018+数位

本文介绍了一个关于书架分配的问题,需要将一定数量的书籍分配到多个书架上,每个书架的价值定义为该书架上所有书籍价格之和。目标是在满足特定条件的情况下最大化所有书架价值的按位与结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 k

bookshelves. He decided that the beauty of the k

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 k

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 n

and k ( 1kn50

) — the number of books and the number of shelves in the new office.

The second line contains n

integers 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 k

shelves in the new office.

Examples
Input
Copy
10 4
9 14 28 1 7 13 15 29 2 31
Output
Copy
24
Input
Copy
7 3
3 14 15 92 65 35 89
Output
Copy
64
Note

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

(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.

#define happy

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;


#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)

#define all(a) (a).begin(),(a).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second



const ll INF=1e18;
const int MOD=1e9+7;
const int N=50+5;

ll rd(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

int n,k;
ll a[N],s[N];
bool dp[N][N];

bool check(ll w){
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;
    rep(i,0,n-1)rep(j,0,k)if(dp[i][j])rep(l,i+1,n)
    if(((s[l]-s[i])&w)==w)dp[l][j+1]=1;
    return dp[n][k];
}


int main(){
#ifdef happy
    freopen("in.txt","r",stdin);
#endif
    n=rd(),k=rd();
    ll ans=0;
    rep(i,1,n)a[i]=rd(),s[i]=s[i-1]+a[i];
    per(i,0,60)if(check(ans|(1ll<<i)))ans|=(1ll<<i);
    printf("%lld\n",ans);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值