Pagini recente » Cod sursa (job #98059) | Cod sursa (job #327523)
Cod sursa(job #327523)
#include <stdio.h>
#define maxn 10010
#define maxk 1001
long n, i, j, k, v[maxn], s[maxn], m, sol;
long d[maxk][maxn];
long max(long a, long b)
{
if(a<b) return b;
return a;
}
int main()
{
freopen("ferma.in", "r", stdin);
freopen("ferma.out", "w", stdout);
scanf("%d%d", &n, &k);
for(i=1; i<=n; i++)
{
scanf("%d", &v[i]);
s[i]=s[i-1]+v[i];
}
for(i=1; i<=k; i++)
{
for(j=0; j<=n; j++)
{
d[i][j]=-1100000000;
}
}
for(i=1; i<=k; i++)
{
m=-1100000000;
for(j=i; j<=n; j++)
{
if(d[i-1][j-1]-s[j-1]>m) m=d[i-1][j-1]-s[j-1];
d[i][j]=max(s[j]+m, d[i][j-1]);
}
}
sol=d[k][n];
printf("%d\n", d[k][n]);
return 0;
}