//这题的方程不会列,看了别人解题报告才懂
//设dp[i][j]为i段以a[j]结尾子序列的最大和
//dp[i][j] = max(dp[i][j-1]+a[j], dp[i-1][k]+a[j])
//由于题目规模巨大,不能使用二维数组存,需要优化
//经分析,可以优化成两个一维数组
//dp[j]为以a[j]结尾的子序列最大和,pre[j]则是记录前个段(即第i-1段)到a[j-1]为止的最大值
//dp[j] = max(dp[j-1]+a[j], pre[j-1]+a[j])
#include <iostream>
using namespace std;
const int N = 1000005;
const int INF = 1000000000;
int dp[N];
int pre[N];
int a[N];
int main()
{
int n, m;
int maxx;
while (scanf("%d%d", &m, &n) != EOF)
{
for (int i = 1; i <= n; i++)
{
dp[i] = 0;
pre[i] = 0;
scanf("%d", &a[i]);
}
dp[0] = 0;
pre[0] = 0;
for (int i = 1; i <= m; i++)
{
maxx = -INF;
for (int j = i; j <= n; j++)
{
dp[j] = max (dp[j-1]+a[j], pre[j-1]+a[j]);
pre[j-1] = maxx;
maxx = max(dp[j], maxx);
}
}
printf("%d\n", maxx);
}
return 0;
}