Cod sursa(job #2071932)

Utilizator tgm000Tudor Mocioi tgm000 Data 21 noiembrie 2017 10:32:20
Problema Carnati Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include<cstdio>
#include<algorithm>
using namespace std;
struct pers{int t,p;};
pers v[2001];
bool cmp(pers a,pers b){
   if(a.t<b.t)
      return true;
   else
      return false;
}
int t[2001],p[2001];
long long prof[2001];
int main(){
   int n,c,i,j;
   long long maxp,maxst,maxdr;
   freopen("carnati.in","r",stdin);
   freopen("carnati.out","w",stdout);
   scanf("%d%d",&n,&c);
   for(i=1;i<=n;i++)
      scanf("%d%d",&v[i].t,&v[i].p);
   sort(v+1,v+n+1,cmp);
   for(i=1;i<=n;i++){
      t[i]=v[i].t;
      p[i]=v[i].p;
   }
   maxp=0;
   for(i=1;i<=n;i++){
      prof[i]=0;
      maxst=0;
      for(j=i-1;j>=1;j--){
         prof[j]=prof[j+1];
         prof[j]-=(t[j+1]-t[j])*c;
         if(p[j]>=p[i])
            prof[j]+=p[i];
         if(prof[j]>maxst)
            maxst=prof[j];
      }
      maxdr=0;
      for(j=i+1;j<=n;j++){
         prof[j]=prof[j-1];
         prof[j]-=(t[j]-t[j-1])*c;
         if(p[j]>=p[i])
            prof[j]+=p[i];
         if(prof[j]>maxdr)
            maxdr=prof[j];
      }
      if(maxst+maxdr+p[i]-c>maxp)
         maxp=maxst+maxdr+p[i]-c;
   }
   printf("%lld",maxp);
   return 0;
}