Cod sursa(job #2408640)

Utilizator bluestorm57Vasile T bluestorm57 Data 18 aprilie 2019 10:45:53
Problema Orase Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

ifstream f("orase.in");
ofstream g("orase.out");

const int Nmax = 1000005;
int mat[2][Nmax];
bool vf[Nmax];
int n,m;
vector<int>v;

int main(){
    int i,j;
    int d,l;
    f >> n >> m;
    for(i = 1 ; i <= m ; i++){

        f >> d >> l;

        if(mat[0][d] <= l){
            mat[1][d] = mat[0][d];
            mat[0][d] = l;
        }else
            if(mat[1][d] < l){
                mat[1][d] = l;
            }

        if(!vf[d]){
            vf[d] = 1;
            v.push_back(d);
        }

    }

    sort(v.begin(), v.end());

    int pos = 0, ans = 0;
    if(mat[0][v[0]] && mat[1][v[0]] && mat[0][v[0]] + mat[1][v[0]] > ans){
        ans = mat[0][v[0]] + mat[1][v[0]];
    }

    int sum;

    for(i = 1 ; i <= v.size(); i++){
        sum = mat[0][v[pos]] + mat[0][v[i]] + v[i] - v[pos];

        ans = max(ans, sum);

        sum -= mat[0][v[i]];

        if(max(sum, mat[0][v[i]]) == mat[0][v[i]])
            pos = i;

        if(mat[0][v[i]] && mat[1][v[i]] && mat[0][v[i]] + mat[1][v[i]] > ans){
            ans = mat[0][v[i]] + mat[1][v[i]];
            pos = i;
        }
    }

    g << ans;

    return 0;
}