Cod sursa(job #142725)

Utilizator Binary_FireFlorin Pogocsan Binary_Fire Data 25 februarie 2008 01:46:06
Problema Carnati Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>

using namespace std;

#define fin  "carnati.in"
#define fout "carnati.out"

const int Nmax = 2010;

int N,C,best,a[Nmax];
vector < pair <int,int> > v;

void readsolve()
{
    int i,j,X,x,y,minv,val;
    
    freopen("carnati.in","r",stdin);
    freopen("carnati.out","w",stdout);
    
    scanf("%d%d",&N,&C);
    
    minv = 100000000;
    
    for ( i = 1; i <= N; ++i )
    {
        scanf("%d%d",&x,&y),v.push_back(make_pair(x,y)); 
        if ( x < minv ) minv = x;
    }
    v.push_back(make_pair(minv-1,0));
    
    sort(v.begin(),v.end());
    
    best = v[1].second - C;
    
    for ( i = 1; i <= N; ++i )
    {
        X = v[i].second;
        
        memset(a,0,sizeof(a));
        
        for ( j = 1; j <= N; ++j )
        {
            if ( X <= v[j].second ) 
               val = X;
            else 
               val = 0; 
            
            a[j] = max( a[j-1] - ( v[j].first - v[j-1].first ) * C + val,val-C);    
            
            best = max(best,a[j]);
        }    
    }
    
	printf("%d\n",best);
}

int main()
{
	readsolve();
	return 0;
}