Pagini recente » Cod sursa (job #49514) | Cod sursa (job #2362071) | Cod sursa (job #929517) | Cod sursa (job #639948) | Cod sursa (job #15139)
Cod sursa(job #15139)
#include <stdio.h>
#define MAXN 34569
int N, N2, T;
int x[MAXN], y[MAXN], d[MAXN], s[MAXN];
long long best[MAXN];
int main()
{
freopen("euro.in", "rt", stdin);
freopen("euro.out", "wt", stdout);
scanf("%d %d", &N, &T);
int i, S = 0;
s[ N2 = 0 ] = 0;
for (i = 1; i <= N; i++)
{
scanf("%d", x + i);
S += x[i]; //atata timp cat am in cont o valoare pozitiva nu voi obtine profitul maxim daca fac conversie
if (S < 0) //abia atunci cand obtin in cont o valoare negativa trebuie sa iau in considerare conversia
{ //=> comprim intervalele cu valori pozitive
y[ ++N2 ] = S;
s[ N2 ] = s[ N2 - 1 ] + S;
d[ N2 ] = i;
S = 0;
}
}
if (d[ N2 ] != N)
{
y[ ++N2 ] = S;
s[ N2 ] = s[ N2 - 1 ] + S;
d[ N2 ] = N;
}
best[0] = 0;
int lst = 1;
for (i = 1; i <= N2; i++)
{
best[i] = -(1LL << 60);
int j; long long S2 = 0;
for (j = lst; j <= i; j++)
{
S2 = (long long)d[i] * (s[i] - s[j - 1]);
if (best[j - 1] + S2 - T > best[i])
{
best[i] = best[j - 1] + S2 - T;
lst = i;
}
}
}
printf("%lld\n", best[ N2 ]);
return 0;
}