E. Maximum Subsequence
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array a consisting of n integers, and additionally an integer m. You have to choose some sequence of indicesb1, b2, ..., bk (1 ≤ b1 < b2 < ... < bk ≤ n) in such a way that the value of  is maximized. Chosen sequence can be empty.

Print the maximum possible value of .

Input

The first line contains two integers n and m (1 ≤ n ≤ 35, 1 ≤ m ≤ 109).

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Output

Print the maximum possible value of .

Examples
input

Copy
4 4
5 2 4 1
output

Copy
3
input

Copy
3 20
199 41 299
output

Copy
19
Note

In the first example you can choose a sequence b = {1, 2}, so the sum  is equal to 7 (and that's 3 after taking it modulo 4).

In the second example you can choose a sequence b = {3}.

/*
折半搜索,把取模后的和存起来
双指针统计答案
*/
#include<bits/stdc++.h> #define N 300000 using namespace std;
int a[N],p[N],q[N];
int k,t,ans,n,m,b,dep,flag; inline int max(int x,int y){return x>y? x:y;} inline int read()
{
int x=,f=;char c=getchar();
while(c>''||c<''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} void dfs(int now,int sum)
{
if(now==dep)
{
if(!flag)
{
p[++k]=sum,p[++k]=(sum+a[b])%m;return;
}
else
{
q[++t]=sum,q[++t]=(sum+a[n])%m;
return ;
}
}
dfs(now+,sum);
dfs(now+,(sum+a[now])%m);
} int main()
{
n=read(),m=read(),b=n>>;dep=b;
for(int i=; i<=n; ++i) a[i]=read();
if(n==) printf("%d",a[]%m),exit();
flag=;dfs(,);
dep=n;flag=;dfs(b+,);
int L=,R=t;
sort(p+,p+k+);sort(q+,q+t+);
while(L<=k)
{
while(p[L]+q[R]>=m) --R;
ans=max(ans,p[L]+q[R]),++L;
}
ans=max(ans,p[k]+q[t]-m);
printf("%d",ans);
return ;
}

最新文章

  1. BZOJ 1047 二维单调队列
  2. Elasticsearch入门必备——ES中的字段类型以及常用属性
  3. PHP之MVC学习
  4. ArrayList和LinkList区别
  5. php异常处理示例
  6. java递归所有文件
  7. MongoDb笔记(一)
  8. 英伟达CUVID硬解,并通过FFmpeg读取文件
  9. 使用PageHepler分页
  10. servlet增删改查
  11. 变量[^_^][T_T]
  12. Nginx 关键字详解
  13. OC-字符串中大小写字母转换
  14. ELK批量删除索引
  15. java阳历转农历
  16. window 计算机 开启事务
  17. queue消息队列
  18. kaggle Titanic
  19. 【转】startActivityForResult和setResult详解
  20. puppet多环境配置(puppet自动化系列2)

热门文章

  1. Swift--错误集:couldn’t be opened because you don’t have permission to view it
  2. list如何remove
  3. web应用启动的时候SpringMVC容器加载过程
  4. SQL2008安装时,“provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接) (.Net SqlClient Data Provider)” 错误的解决方案
  5. 搭建网络svn实战
  6. [PythonCode]扫描局域网的alive ip地址
  7. [RxJS] `add` Inner Subscriptions to Outer Subscribers to `unsubscribe` in RxJS
  8. Openstack-Ceilometer-获取主机内存 的使用
  9. 【bzoj2753】[SCOI2012]滑雪与时间胶囊
  10. Test redis