Pagini recente » Cod sursa (job #2151366) | Cod sursa (job #376548) | Cod sursa (job #171401) | Cod sursa (job #2045286) | Cod sursa (job #168019)
Cod sursa(job #168019)
#include <stdio.h>
#define max(a, b) ((a) > (b) ? (a) : (b))
int n, c;
int t[2001], p[2001];
long long int a[2001];
int i, j, k, g;
long long int prof = 0;
void Build(int x);
void Qsort(int st, int dr);
int main()
{
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]);
Qsort(1, n);
for ( i = 1; i <= n; i++ )
Build(p[i]);
printf("%d\n", prof);
return 0;
}
void Qsort(int st, int dr)
{
int i = st-1;
int j = dr+1;
do
{
do { i++; } while ( t[i] < t[st] );
do { j--; } while ( t[st] < t[j] );
if ( i <= j )
{
int aux = t[i]; t[i] = t[j];t[j] = aux;
aux = p[i]; p[i] = p[j]; p[j] = aux;
}
} while ( i <= j );
if ( st < j ) Qsort(st, j);
if ( i < dr ) Qsort(i, dr);
}
void Build(int x)
{
for ( j = 1; j <= n; j++ )
g = (x <= p[j] ? x : 0),
a[j] = max(a[j-1]-(t[j] - t[j-1]) * c + g, g-c),
prof = prof < a[j] ? a[j] : prof;
}