Pagini recente » Cod sursa (job #1036482) | Cod sursa (job #1003717) | Cod sursa (job #2395004) | Cod sursa (job #292429) | Cod sursa (job #750509)
Cod sursa(job #750509)
#include <cstdio>
#include <cmath>
#define nmax 34600
#define inf -1000000000000000LL
int n, t, c[nmax], h, s[nmax], p[nmax];
long long sol[nmax];
inline int max (int a, int b)
{
if (a < b) a = b;
return a;
}
int main()
{
freopen ("euro.in", "r", stdin);
freopen ("euro.out", "w", stdout);
scanf ("%d %d", &n, &t);
int i, j, r;
for (i = 1; i <= n; i++) scanf ("%d ", &c[i]);
h = 1;
for (i = 1; i <= n; i++)
{
if (s[h] >= 0) s[h] += c[i]; else
s[++h] = c[i];
p[h] = i;
}
for (i = 1; i <= h; i++) s[i] += s[i-1];
r = sqrt (t);
for (i = 1; i <= h; i++)
{
sol[i] = -inf;
for (j = max (0, i-r); j < i; j++)
sol[i] = max (sol[i], sol[j] + (long long) (s[i]-s[j]) * (long long) p[i] - t);
}
printf ("%lld\n", sol[h]);
}