Cod sursa(job #23609)

Utilizator alextheroTandrau Alexandru alexthero Data 1 martie 2007 06:08:34
Problema Euro Scor 85
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <cmath>

#define LL long double
#define inf 1e-4 
#define nmax 35000
#define pb push_back
#define mp(i,j) make_pair(i,j)

using namespace std;

int i,j,n;
LL t,s;
LL a[nmax],sp[nmax],r[nmax];
vector <pair <int,int> > v;

LL suma(int i,int j) {
	if(i == 0 || j == 0) return 0;
	return (LL)sp[j] - sp[i - 1];
}

int main() {
	freopen("euro.in","r",stdin);
	freopen("euro.out","w",stdout);
	
	scanf("%d",&n);
	scanf("%Lf",&t);

	for(i = 1; i <= n; i++) scanf("%Lf",&a[i]);

	LL s = 0;
	int prev = 0;
	v.pb(mp(0,0));
	for(i = 1; i <= n; i++) {
		sp[i] = (LL)(sp[i - 1] + a[i]);
		s += a[i];
		if(i == n || s < 0) {
	    	v.pb(mp(prev + 1,i));
			s = 0;
			prev = i;
		}
	}

	int n1 = v.size();
	r[0] = (LL)suma(v[0].first,v[0].second) * v[0].second;
	for(i = 1; i < n1; i++) {
		r[i] = -2000000000;
		for(j = max(0,i - (int)sqrt(t) - 30); j < i; j++) 
			r[i] = (LL)max(r[i],(LL)r[j] - t + suma(v[j].second + 1,v[i].second) * v[i].second);	
	}
 
	printf("%.0Lf\n",r[n1 - 1]);
	return 0;
}