Cod sursa(job #1216335)

Utilizator ionut98Bejenariu Ionut Daniel ionut98 Data 4 august 2014 10:50:15
Problema Orase Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.87 kb
#include<fstream>
#include<algorithm>
using namespace std;
ifstream f("orase.in");
ofstream g("orase.out");
unsigned long long n,m,i,max1,maxd,sum,no,lg,sum2,dist;
struct pie
{
    int l,d;
}a[50001];
int cmp(pie a,pie b)
{
    if(a.l!=b.l)
        return a.l<b.l;
    else
        return a.d<b.d;
}
int main()
{
    f>>m>>n;
    for(i=1;i<=n;i++)
      f>>a[i].l>>a[i].d;

    sort(a+1,a+n+1,cmp);
    for(i=1;i<=n;i++)
        if(maxd<a[i].d)
        {
            maxd=a[i].d;
            no=i;
            lg=a[i].l;
        }
    sum=maxd;
    for(i=1;i<=n;i++)
    {
        if(i!=no)
        {
            sum2=a[i].d;
            dist=a[i].l-lg;
            if(dist<0)
            dist=dist*-1;
            sum2+=dist;
        }
        if(sum2>max1)
        max1=sum2;
        sum2=0;
    }
    g<<max1+sum<<"\n";
    return 0;
}