Pagini recente » Cod sursa (job #1260177) | Cod sursa (job #259790) | Cod sursa (job #1270394) | Cod sursa (job #1740323) | Cod sursa (job #369395)
Cod sursa(job #369395)
#include <fstream>
using namespace std;
ifstream f ("orase.in");
ofstream g ("orase.out");
long long m, n, v[100000][2], w[100000][2];
void read()
{ f >> m >> n;
for (int i=1; i<=n; i++)
{ f >> v[i][0] >> v[i][1];
if (v[i][0]>m) { v[i][0]=0; v[i][1]=0; }
}
}
void quicksort(int low, int high)
{ long long x=v[(low+high)/2][0];
int i=low, j=high;
long long h;
do
{
while (v[i][0]<x) i++;
while (v[j][0]>x) j--;
if (i<j)
{ h=v[i][0]; v[i][0]=v[j][0]; v[j][0]=h;
h=v[i][1]; v[i][1]=v[j][1]; v[j][1]=h;
}
i++; j--;
}while (i<j);
if (j>low) quicksort(low, j);
if (i<high) quicksort(i, high);
}
void program()
{ long long max=v[1][1]+v[2][1]+v[2][0]-v[1][0];
int i=1, j=2;
for (int k=3; k<=n; k++)
{ int ci=i, cj=j;
if (v[ci][1]+v[k][1]+v[k][0]-v[ci][0]>max)
{ max=v[ci][1]+v[k][1]+v[k][0]-v[ci][0];
j=k;
}
if (v[cj][1]+v[k][1]+v[k][0]-v[cj][0]>max)
{ max=v[cj][1]+v[k][1]+v[k][0]-v[cj][0];
i=cj;
j=k;
}
}
g << max;
}
int main()
{ read();
quicksort(1, n);
program();
f.close();
g.close();
return 0;
}