-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD.cpp
57 lines (45 loc) · 1.12 KB
/
D.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
56
57
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
const int64_t zero = 0;
void run_cases() {
int N;
cin >> N;
vector<vector<int64_t>> items(N, vector<int64_t>(2));
for(int i = 0; i < N; i++) {
cin >> items[i][0] >> items[i][1];
}
sort(items.begin(), items.end(), [&](auto &a, auto &b) {
return a[1] < b[1];
});
int64_t discounted = 0, current = 0;
int64_t total = 0;
for(auto u: items) {
total += u[0];
}
int l = 0, r = N - 1;
while(l <= r) {
int64_t req = max(zero, items[l][1] - current);
if(req > 0) {
int64_t got = min(req, items[r][0]);
current += got;
items[r][0] -= got;
if(items[r][0] == 0) {
r--;
}
} else {
discounted += items[l][0];
current += items[l][0];
l++;
}
}
cout << 2 * total - discounted << '\n';
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(nullptr);
int tests = 1;
// cin >> tests;
for(int test = 1;test <= tests;test++) {
run_cases();
}
}