Cod sursa(job #23528)

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

#define inf 2000000000
#define nmax 35000
#define pb push_back
#define mp(i,j) make_pair(i,j)

using namespace std;

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

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

int main() {
	freopen("euro.in","r",stdin);
	freopen("euro.out","w",stdout);
	
	scanf("%d %d\n",&n,&t);
	for(i = 1; i <= n; i++) scanf("%d",&a[i]);

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

	int n1 = v.size();
	r[0] = suma(v[0].first,v[0].second) * v[0].second;
	for(i = 1; i < n1; i++) {
		r[i] = -inf;
		for(j = 0; j < i; j++) 
			r[i] = max(r[i],r[j] - t + suma(v[j].second + 1,v[i].second) * v[i].second);	
	}

	printf("%d\n",r[n1 - 1]);
	return 0;
}