-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathicpc2016L.cpp
More file actions
59 lines (50 loc) · 1.32 KB
/
icpc2016L.cpp
File metadata and controls
59 lines (50 loc) · 1.32 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
struct cmpByGain {
bool operator() (pair<int, int> a, pair<int, int> b) {
return a.first < b.first;
}
};
struct cmpByGain2 {
bool operator() (pair<int, int> a, pair<int, int> b) {
return a.second + a.first > b.second + b.first;
}
};
int main() {
int n, oldc, newc;
while(scanf("%d", &n) != EOF) {
vector< pair<int, int> > diskG, diskL;
for(int i = 0; i < n; ++i) {
scanf("%d%d", &oldc, &newc);
if(newc > oldc)
diskG.push_back(make_pair(oldc, newc-oldc));
else
diskL.push_back(make_pair(oldc, newc-oldc));
}
sort(diskG.begin(), diskG.end(), cmpByGain());
sort(diskL.begin(), diskL.end(), cmpByGain2());
long long capc = 0;
long long requirenew = 0;
for(int i = 0; i < diskG.size(); ++i) {
if(diskG[i].first > capc) {
requirenew += diskG[i].first - capc;
capc = diskG[i].first + diskG[i].second;
} else {
capc += diskG[i].second;
}
}
for(int i = 0; i < diskL.size(); ++i) {
if(diskL[i].first > capc) {
requirenew += diskL[i].first - capc;
capc = diskL[i].first + diskL[i].second;
} else {
capc += diskL[i].second;
}
}
printf("%lld\n", requirenew);
}
return 0;
}