Cod sursa(job #183512)

Utilizator toni2007Pripoae Teodor Anton toni2007 Data 22 aprilie 2008 12:18:46
Problema Carnati Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <stdio.h>
#include <stdlib.h>
#define N 2010
int t[N],p[N],n,c,max=0;
int maxim(int a,int b){
	if (a>b)
      return a;
    return b;
}
void scan(){
	int i;
	freopen("carnati.in","r",stdin);
    freopen("carnati.out","w",stdout);
    scanf("%d%d",&n,&c);
    for (i=1;i<=n;++i)
       scanf("%d%d",&t[i],&p[i]);
}
void swap(int i,int j){
	int aux;
    aux=t[i];
    t[i]=t[j];
    t[j]=aux;
    aux=p[i];
    p[i]=p[j];
    p[j]=aux;
}
void down_heap(int i,int n){
	int max=i;
    if (2*i<=n && t[max]<t[2*i])
      max=2*i;
    if (2*i+1<=n && t[max]<t[2*i+1])
      max=2*i+1;
    if (max!=i){
      swap(i,max);
      down_heap(max,n);
    }
}
void sort(){
	int i;
    for (i=n/2;i>=1;--i)
       down_heap(i,n);
    for (i=n;i>1;--i){
       swap(1,i);
       down_heap(1,i-1);
    }
}
void solve(){
	int i,j,last,now;
    t[0]=p[0]=-10;
    for (i=1;i<=n;++i){
       last=now=0;
       for (j=1;j<=n;++j){
          	if (p[j]>=p[i])
              now=maxim(last-(t[j]-t[j-1])*c+p[i],p[i]-c);
            else
              now=maxim(last-(t[j]-t[j-1])*c,-c);
            if (now>max)
              max=now;
            last=now;
       }
	}
}
void print(){
	printf("%d\n",max);
	fclose(stdin);
    fclose(stdout);
    exit(0);
}
int main(){
     scan();
     sort();
     solve();
     print();
}