Cod sursa(job #3361701)

Utilizator Horea_88Bodea Horea Florin Horea_88 Data 27 iulie 2026 19:13:45
Problema Orase Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <iostream>
#include <fstream>

using namespace std;

long long D[50005], L[50005];

void sorteaza(int n) {
    for (int i = 1; i < n; i++) {
        for (int j = i + 1; j <= n; j++) {
            if (D[i] > D[j]) {
                long long auxD = D[i];
                D[i] = D[j];
                D[j] = auxD;

                long long auxL = L[i];
                L[i] = L[j];
                L[j] = auxL;
            }
        }
    }
}

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

    long long M;
    int N;
    fin >> M >> N;

    for (int i = 1; i <= N; i++) {
        fin >> D[i] >> L[i];
    }

    sorteaza(N);

    long long distanta_maxima = 0;
    long long max_dif = L[1] - D[1];

    for (int i = 2; i <= N; i++) {
        long long curent = L[i] + D[i] + max_dif;
        if (curent > distanta_maxima) {
            distanta_maxima = curent;
        }
        if (L[i] - D[i] > max_dif) {
            max_dif = L[i] - D[i];
        }
    }

    fout << distanta_maxima << "\n";

    fin.close();
    fout.close();
    return 0;
}