forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathG.cpp
More file actions
45 lines (40 loc) · 932 Bytes
/
G.cpp
File metadata and controls
45 lines (40 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>
using namespace std;
const long long oo = 1LL << 60;
int n;
pair <long long,long long> a[100100];
int main()
{
freopen("pulp.in", "r", stdin);
freopen("pulp.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i].first >> a[i].second;
sort(a, a + n);
long long ans = 0, t = 0;
set < pair<long long,long long> > s;
for (int i = 0; i < n || !s.empty();)
{
long long dif = i < n ? a[i].first - t : oo;
if (!s.empty() && s.begin() -> first <= dif)
{
t += s.begin() -> first;
ans += t;
s.erase(s.begin());
}
else
{
if (!s.empty())
{
auto u = s.begin();
s.erase(u);
s.insert({u -> first - dif, u -> second});
}
s.insert({a[i].second, i});
t = a[i++].first;
}
}
cout << ans << endl;
}