Pagini recente » Cod sursa (job #2572323) | Cod sursa (job #1420696) | Cod sursa (job #2998778) | Cod sursa (job #2594163) | Cod sursa (job #3125457)
#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 > early;
SUM += (it->second - it->first);
early.first = it->first;
early.second = it->second;
while(it != my_set.end())
{
advance(it, 1);
if(early.first == it->first)
{
if(early.second < it->second)
{
SUM += it->second - early.second;
early.second = it->second;
}
}
else if(early.second > it->first)
{
if(it->second - it->first > early.second - early.first)
{
SUM -= (early.second - early.second), SUM += it->second - it->first;
early.first = it->first;
early.second = it->second;
}
}
else
{
SUM += it->second - it->first;
early.first = it->first;
early.second = it->second;
}
}
fout << SUM;
}
int main()
{
ios_base::sync_with_stdio(false);
Task();
return 0;
}