Cod sursa(job #1658718)

Utilizator andy1207Cioltan Andrei andy1207 Data 21 martie 2016 19:10:00
Problema Orase Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
struct qwer{int d,l;};
qwer v[50001];
bool comparare(qwer a,qwer b)
{
 if(a.d>b.d)
    return false;
 return true;
}
int main()
{
 int n,m,i,poz,s,max;
 freopen("orase.in","r",stdin);
 freopen("orase.out","w",stdout);
 scanf("%d%d",&m,&n);
 for(i=1;i<=n;i++)
    {
     scanf("%d%d",&v[i].d,&v[i].l);
    }
 sort(v+1,v+n+1,comparare);
 max=v[1].d+v[1].l;
 poz=0;
 for(i=2;i<=n;i++)
    {
     if(poz!=0)
        {
         s=v[i].d-v[poz].d+v[i].l+v[poz].l;
         if(s>max)
            {
             max=s;
            }
        }
     s=v[i].d-v[i-1].d+v[i].l+v[i-1].l;
     if(s>max)
        {
         max=s;
         if(v[i].d==v[i-1].d)
            {
             if(v[i].l>v[i-1].l)
                poz=i;
             else
                poz=i-1;
            }
         else
            {
             poz=i;
            }
        }
    }
 printf("%d\n",max);
return 0;
}