Cod sursa(job #2690976)

Utilizator WilIiamperWilliam Damian Balint WilIiamper Data 26 decembrie 2020 16:45:00
Problema Heavy metal Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <fstream>
#include <algorithm>

using namespace std;

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

int n, DP[1001][1001], sol;
pair <int, int> a[100001];

bool compare ( pair <int, int> x, pair <int, int> y ) {

    if ( x.first < y.first )
        return true;
    else if ( x.first == y.first ) {
        if ( x.second < y.second )
            return true;
        else return false;
    }
    return false;
}

int main()
{
    fin >> n;
    for (int i = 1; i <= n; i++) {
        fin >> a[i].first >> a[i].second;
    }

    sort ( a+1, a+n+1, compare );

    sol = 0;
    for ( int i = 1; i <= n; i++ ) {

        for (int j = 1; j <= a[n].second; j++) {
            DP[i][j] = max ( DP[i-1][j], DP[i][j-1] );

            if ( j == a[i].second )
                DP[i][j] = max ( DP[i][j], DP[i-1][ a[i].first ] + ( a[i].second - a[i].first ) );
        }

        sol = max ( sol, DP[i][ a[n].second ] );
    }
    fout << sol << "\n";
    return 0;
}