-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8980.cpp
70 lines (65 loc) · 1.41 KB
/
8980.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
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <bits/stdc++.h>
using tiii = std::tuple<int,int,int>;
using pii = std::pair<int,int>;
void solve(){
int c,N, K; std::cin >> c>>K >> N;
tiii arr[N]; for (auto &[a,b,c] : arr) std::cin >> a>>b>>c;
auto compare = [](tiii a, tiii b){
auto [x1,x2,x3] = a;
auto [y1,y2,y3] = b;
if (x1 == y1)
return x2 <= y2;
return x1 <= y1;
};
sort(arr,arr+N,compare);
std::multiset<pii> s;
int cur = 0;
int result = 0;
for (int i = 0; i < N; i++) {
auto [a,b,c] = arr[i];
while (s.size()) {
auto [x1,x2] = *s.begin();
if (x1 > a) break;
s.erase(s.begin());
cur -= x2;
result += x2;
}
if (cur + c <= K) {
cur += c;
s.insert({b,c});
} else {
while (s.size()) {
auto it = --s.end();
auto [x1,x2] = *it;
if (b >= x1) {
break;
} else {
if (K - cur >= c) break;
if (K - cur + x2 <= c) {
s.erase(it);
cur -= x2;
} else {
int r = c - (K - cur);
s.erase(it);
s.insert({x1, x2 - r});
cur -= r;
break;
}
}
}
int n = std::min(K - cur, c);
if (!n) continue;
cur += n;
s.insert({b, n});
}
}
for (auto [a,b] : s) {
result += b;
}
std::cout << result;
}
int main(){
std::cin.tie(0)->sync_with_stdio(false);
solve();
return 0;
}