Pagini recente » Cod sursa (job #1368609) | Cod sursa (job #1965488) | Cod sursa (job #2874065) | Cod sursa (job #513276) | Cod sursa (job #137823)
Cod sursa(job #137823)
#include <stdio.h>
#include <stdlib.h>
int max ( int *t, long *p, int n,long c,long cunic) {
long * cost;
cost = ( long * ) malloc ( (n+1) * sizeof(long));
if (p[0]>cunic) cost [0] = cunic - c; else cost [0] =-c;
int i;
for ( i = 1; i<n; i++)
if ( p[i] < cunic ) cost [i] = cost [i-1] - c*(t[i]-t[i-1]);
else
{ cost[i]=cunic-c;
if ( cost [i-1] - c * (t[i]-t[i-1]-1) > 0)
cost[i]+= cost [i-1] - c * (t[i]-t[i-1]-1) ;
}
long maxim=0;
for (i=0;i<n;i++) if (maxim<cost[i]) maxim=cost[i];
return maxim;
}
int main() {
FILE *f=fopen("carnati.in","r");
int n,i;long c;
long *p;int *t;
fscanf(f,"%d%ld\n",&n,&c);
p = ( long * ) malloc ( sizeof(long) * (n+1));
t = ( int * ) malloc ( sizeof(int) * (n+1));
for ( i=0; i<n;i++) fscanf(f,"%d%ld\n",t+i,p+i);
long maximul=0,total;
for (i=0;i<n;i++)
{
total = max ( t,p,n,c,p[i]);
printf("%ld\n",total);
if (total > maximul) maximul=total;
}
fclose(f);
f=fopen("carnati.out","w");
fprintf(f,"%ld",maximul);
fclose(f);
return 0;
}