Cod sursa(job #2060158)

Utilizator dragomirmanuelDragomir Manuel dragomirmanuel Data 7 noiembrie 2017 21:51:18
Problema Heavy metal Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <iostream>
#include <algorithm>
#include <fstream>

using namespace std;

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

const int NMax = 100005;
const int inf = 0x3f3f3f3f;
int N;

struct Trupa
{
    int low, high;
}v[NMax];

void Read()
{
    fin >> N;
    for(int i=1; i<=N; ++i)
        fin >> v[i].low >> v[i].high;
}

bool cmp(Trupa A, Trupa B)
{
    return A.high < B.high;
}

void Solve()
{
    sort(v+1, v+N+1, cmp);
    int sum = 0, maxi = v[1].high - v[1].low;
    for(int i=2; i<=N; ++i)
    {
        if(v[i-1].high <= v[i].low)
        {
            sum += maxi;
            maxi = v[i].high - v[i].low;
        }

        else
            maxi = max(maxi, v[i].high - v[i].low);
    }

    sum += maxi;

    fout << sum;
}

int main()
{
    Read();
    Solve();
    return 0;
}