Ridhiman challenged Ashish to find the maximum valued subsequence of an array a of size n consisting of positive integers.

The value of a non-empty subsequence of k elements of a is defined as ∑2i over all integers i≥0 such that at least max(1,k−2) elements of the subsequence have the i-th bit set in their binary representation (value x has the i-th bit set in its binary representation if ⌊x2i⌋mod2 is equal to 1).

Recall that b is a subsequence of a, if b can be obtained by deleting some(possibly zero) elements from a.

Help Ashish find the maximum value he can get by choosing some subsequence of a.

Input
The first line of the input consists of a single integer n (1≤n≤500) — the size of a.

The next line consists of n space-separated integers — the elements of the array (1≤ai≤1018).

Output
Print a single integer — the maximum value Ashish can get by choosing some subsequence of a.

Examples
Input
3
2 1 3
Output
3
Input
3
3 1 4
Output
7
Input
1
1
Output
1
Input
4
7 7 1 1
Output
7
Note
For the first test case, Ashish can pick the subsequence {2,3} of size 2. The binary representation of 2 is 10 and that of 3 is 11. Since max(k−2,1) is equal to 1, the value of the subsequence is 20+21 (both 2 and 3 have 1-st bit set in their binary representation and 3 has 0-th bit set in its binary representation). Note that he could also pick the subsequence {3} or {2,1,3}.

For the second test case, Ashish can pick the subsequence {3,4} with value 7.

For the third test case, Ashish can pick the subsequence {1} with value 1.

For the fourth test case, Ashish can pick the subsequence {7,7} with value 7.

思路:对于n<3的数组来说,我们可以直接暴力求出来。
对于n>=3的数组,我们选择k==3的时候,一定是最优的。因为max(3-2,1)==1.如果原来选择的3个数,第i位有1个1,但是呢,加进来的这个数字,第i个不为0,这样的话,加进来的这个数,不仅没有贡献,还会拉低价值。比4更高的也是一样。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=5e2+10;
ll a[maxx];
int num[maxx];
int n;int main()
{scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%lld",&a[i]);ll ans=0;for(int i=1;i<=n;i++){ans=max(ans,a[i]);for(int j=i+1;j<=n;j++){ans=max(ans,a[i]|a[j]);for(int k=j+1;k<=n;k++) ans=max(ans,a[i]|a[j]|a[k]);}}cout<<ans<<endl;return 0;
}

努力加油a啊,(o)/~

Maximum Subsequence Value CodeForces - 1365E(规律+暴力)相关推荐

  1. PAT甲级真题-1007 Maximum Subsequence Sum详解优化

    1007 Maximum Subsequence Sum 题目链接 https://pintia.cn/problem-sets/994805342720868352/problems/9948055 ...

  2. PAT A 1007. Maximum Subsequence Sum (25)

    原题 Given a sequence of K integers { N1, N2, ..., NK }.  A continuous subsequence is defined to be { ...

  3. 贪心 ---- E. Maximum Subsequence Value[位运算]

    E. Maximum Subsequence Value 题目大意:有点难解释..建议自己看题.我这里就粗略解释:给定一个数组aaa,要求选出具有最大价值的子序列.假设此子序列的长度为kkk,那么最大 ...

  4. 第一周 01-复杂度2 Maximum Subsequence Sum

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  5. PAT甲级1007 Maximum Subsequence Sum :[C++题解]DP,最大子序列和、求最优的区间方案

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: dp题. 这道题糅合了两个知识点: dp求最值(区间之和) 动态求区间方案:区间之和相等的条件下:要求区间左端点最靠前,如果左端点相 ...

  6. 1007 Maximum Subsequence Sum

    1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A c ...

  7. PAT甲级 -- 1007 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  8. 【测试点5】1007 Maximum Subsequence Sum (25 分)

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given a sequence of K integers { N​1​​ , N​2​​ , -, N​K​​ }. A co ...

  9. PAT 1007 Maximum Subsequence Sum

    1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A c ...

最新文章

  1. 研究生扩招20.74%!教育部公布重要数据
  2. [ZJOI2019]线段树
  3. 一个包从服务器到达客户端
  4. Linux 的系统运行级别
  5. 记一次阿里云的面试,止步第三轮,可惜!
  6. SDOI2020游记
  7. Java中字符串的常用属性与方法
  8. java基础----Date、DateFormat
  9. 【软件开发底层知识修炼】十四 快速学习GDB调试一 入门使用
  10. mysql导出表数据
  11. tcp/udp/socket 端口映射,转发小工具
  12. LanguageTool最简范例代码
  13. 飞客蠕虫病毒的介绍与处理办法
  14. 阿里月饼门 vs 阿里价值观
  15. java生成opml
  16. python预处理tif格式图片
  17. This application failed to start because it could not find or load the Qt platform plugin “xcb“.
  18. 刷题体验第一天——《录鼎记》第一章
  19. 关于Provision.apk
  20. git本地仓库关联远程仓库的两种方式

热门文章

  1. Python—实训day4—爬虫案例3:贴吧图片下载
  2. ttf能改成gfont吗_中国废弃轮胎,被非洲人买去做成凉鞋!15元一双,至少能穿10年...
  3. python 数字转十六进制_在Python中将整数转换为十六进制
  4. vue中使用js-cookie
  5. Android开发之LayoutInflater.from(context).inflate()方法参数介绍解决RecyclerView加载布局不全的问题
  6. Android控件随手指的移动而移动
  7. linux tomcat 日志乱码,Linux下TOMCAT中日志出现中文乱码
  8. iOS-raywenderlich翻译-AFNetworking速成教程
  9. Oracle一定有sqlplus吗,oracle sqlplus执行sql文件
  10. 我们用5分钟写了一个跨多端项目