-
Notifications
You must be signed in to change notification settings - Fork 2
/
delivery.cpp
55 lines (45 loc) · 1.18 KB
/
delivery.cpp
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
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
long long solve(vector<pair<long long, long long>>& v, long long letters) {
long long lettersleft = letters;
long long traveled = v[v.size()-1].first * 2;
for(int i = v.size()-1; i >= 0; i--) {
if(lettersleft >= v[i].second) {
lettersleft -= v[i].second;
v.pop_back();
}
else {
v[i].second -= lettersleft;
break;
}
}
return traveled;
}
int main() {
long long stops, letters;
cin >> stops >> letters;
vector<pair<long long, long long>> v_pos;
vector<pair<long long, long long>> v_neg;
for(int i = 0; i < stops; i++) {
long long dist, l;
cin >> dist >> l;
if(dist < 0) {
v_neg.push_back({-dist, l});
}
else {
v_pos.push_back({dist, l});
}
}
sort(v_neg.begin(), v_neg.end());
sort(v_pos.begin(), v_pos.end());
long long total = 0;
while(v_neg.size() > 0) {
total += solve(v_neg, letters);
}
while(v_pos.size() > 0) {
total += solve(v_pos, letters);
}
cout << total << endl;
}