Pagini recente » Cod sursa (job #462711) | Cod sursa (job #640026) | Cod sursa (job #2511368) | Cod sursa (job #626320) | Cod sursa (job #1597000)
#include <iostream>
#include <fstream>
#include <stdlib.h>
#define Max(a,b) a>b ? a:b
using namespace std;
fstream f;
struct orase
{
long int dps,ps;
}t[50000],inf,sup;
long int m,n,best[50001];
long int i,j,sumamax,aux;
long int lmax;
int dist (orase a,orase b)
{
return a.dps+b.dps+abs(a.ps-b.ps);
}
void quickSort(orase arr[], int left, int right) {
int i = left, j = right;
orase tmp;
int pivot = arr[(left + right) / 2].ps;
/* partition */
while (i <= j) {
while (arr[i].ps < pivot)
i++;
while (arr[j].ps > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
};
/* recursion */
if (left < j)
quickSort(arr, left, j);
if (i < right)
quickSort(arr, i, right);
}
int main()
{
f.open("orase.in",ios::in);
f>>m>>n;
for (i=0;i<n;i++)
{
f>>t[i].ps>>t[i].dps;
}
quickSort(t,0,n-1);
best[1]=0;
best[2]=dist(t[0],t[1]);
sumamax=best[2];
for (i=3;i<=n;i++)
{
best[i]=Max(best[i-1]-t[i-2].dps+t[i-1].dps+t[i-1].ps-t[i-2].ps, dist(t[i-2],t[i-1]));
sumamax=Max(sumamax,best[i]);
}
cout<<sumamax;
f.close();
f.open("orase.out",ios::out);
f<<sumamax;
f.close();
return 0;
}