Cod sursa(job #3172278)

Utilizator KarinaDKarina Dumitrescu KarinaD Data 20 noiembrie 2023 14:23:36
Problema Orase Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

ifstream fin ( "orase.in" );
ofstream fout ( "orase.out" );

const int N = 1e6;

struct oras {
    long long d, l;
} v[N + 10];

bool cmp ( oras a, oras b ) {
    return a.d > b.d;
}

int main () {
    
    int n, m;
    long long val = 0, ans = 0;
    
    fin >> n >> m;
    
    for ( int i = 0; i < m; i++ )
        fin >> v[i].d >> v[i].l;
    
    sort ( v, v + m, cmp );
    
    val = ans = v[0].l + v[0].d;
    
    for ( int i = 1; i < m; i++ ) {
        ans = max ( ans, val + v[i].l - v[i].d );
        val = max ( val, v[i].l + v[i].d );
    }
    
    fout << ans;
    
    return 0;
}