Cod sursa(job #855260)
#include<stdio.h>
#include<algorithm>
#define x first
#define y second
using namespace std ;
pair < long , long > a [ 50007 ] ;
inline bool cmp ( pair < long , long > a , pair < long , long > b )
{
if ( a . x != b . x )
return a . x < b . x ;
return a . y < b . y ;
}
long c [ 50007 ] ;
long n , m ;
int main ( )
{
freopen ( "orase.in" , "r" , stdin ) ;
freopen ( "orase.out" , "w" , stdout ) ;
scanf ( "%ld %ld" , & m , & n ) ;
for ( long i = 1 ; i <= n ; ++ i )
scanf ( "%ld %ld" , & a [ i ] . x , & a [ i ] . y ) ;
sort ( a + 1 , a + n + 1 , cmp ) ;
for ( long i = 1 ; i <= n ; ++ i )
c [ i ] = a [ i - 1 ] . y + max ( c [ i - 1 ] + 1 , a [ i - 1 ] . x + 1 ) ;
printf ( "%ld" , c [ n ] ) ;
return 0 ;
}