//tyvj 1340 送礼物
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
int n,tp=0,tp2,ans;
long long w;
int a[49],cun[9000001];
bool vis[49];
inline int max(int x,ll y) {if(x>y) return x;return y;}
inline int read()
{
int 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;
}
inline bool cmp(int x,int y) {return x>y;}
inline void dfs(int l,int r,ll now)
{
int i,j;
if(now>w) return;
cun[++tp]=now;
fo(i,l,r)
dfs(i+1,r,(long long)now+a[i]);
}
inline void dfs2(long long now,int step,int dep)
{
if (now>w) return ;
if (step>dep)
{
int l=1,r=tp;
while (l<r)
{
int mid=(l+r)/2+1;
if ((long long)now+cun[mid]>w) r=mid-1;
else l=mid;
}
long long tmp=(long long)now+cun[l];
if (tmp>w||tmp<0) return ;
ans=max(ans,tmp);
return;
}
dfs2(now+a[step],step+1,dep);
dfs2(now,step+1,dep);
}
int main()
{
int i,j;
w=read();n=read();
fo(i,1,n)
a[i]=read();
int mid=n/2;
dfs(1,n/2,0);
sort(cun+1,cun+tp+1);
dfs2(0,n/2+1,n);
printf("%d\n",ans);
return 0;
}