Pagini recente » Cod sursa (job #1437691) | Cod sursa (job #2463959) | Cod sursa (job #3122996) | Cod sursa (job #1479298) | Cod sursa (job #3125455)
#include <iostream>
#include <fstream>
#include <iterator>
#include <set>
using namespace std;
ifstream fin("heavymetal.in");
ofstream fout("heavymetal.out");
int N;
multiset < pair < int, int > > my_set;
void Task()
{
fin >> N;
for(int i = 1; i <= N; ++ i)
{
int A, B;
fin >> A >> B;
my_set.insert(make_pair(A, B));
}
long long SUM = false;
multiset < pair < int, int > > ::iterator it = my_set.begin();
pair < int, int > MH;
SUM += (it->second - it->first);
MH.first = it->first;
MH.second = it->second;
while(it != my_set.end())
{
advance(it, 1);
if(it->first == MH.first)
{
if(it->second > MH.second){
SUM -= MH.second - MH.first;
MH.first = it->first;
MH.second = it->second;
SUM += it->second - it->first;
}
}
else if(it->first < MH.second)
{
if(it->second - it->first > MH.second - MH.first)
{
SUM -= MH.second - MH.first;
MH.first = it->first;
MH.second = it->second;
SUM += it->second - it->first;
}
}
else if(it->first >= MH.second)
{
SUM += it->second - it->first;
MH.first = it->first;
MH.second = it->second;
}
}
fout << SUM;
}
int main()
{
ios_base::sync_with_stdio(false);
Task();
return 0;
}