暴力出奇迹(贪心&暴力)
思路:贪心+枚举,可以知道答案肯定是一段区间的某一位全部为1.这样肯定是最优的。所以我们考虑枚举这个为1的位。然后遇到某个数该位0就更新区间继续枚举。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;
#define mst(a) memset(a,0,sizeof a)
#define lx x<<1
#define rx x<<1|1
#define reg register
#define PII pair<int,int>
#define fi first
#define se second
inline void read(int &x){
x=0;int w=1;
char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
for(;ch>='0'&&ch<='9';ch=getchar())
x=(x<<3)+(x<<1)+(ch&15);
x*=w;
}
ll ans;
int a[N],n;
int main(){
read(n);
for(int i=1;i<=n;i++) read(a[i]);
for(int j=0;j<32;j++){
ll sum=0,tmp=a[1];
for(int i=1;i<=n;i++){
if((a[i]>>j)&1){
sum+=a[i];
tmp&=a[i];
ans=max(ans,sum*tmp);
}
else {
tmp=a[i+1];
sum=0;
}
}
}
printf("%lld\n",ans);
return 0;
}