-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI. Indo ao Mercado.cpp
83 lines (62 loc) · 1.15 KB
/
I. Indo ao Mercado.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
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<typename T = ll>
struct VeniceSet {
using T2 = pair<T, ll>;
priority_queue<T2, vector<T2>, greater<T2>> pq;
T acc;
VeniceSet(): acc() {}
void add_element(const T& e, const ll q) {
pq.emplace(e-acc, q);
}
void update_all(const T& x) {
acc += x;
}
T2 best() {
auto ret = pq.top();
ret.first += acc;
return ret;
}
void pop() {
pq.pop();
}
void pop_k( int k) {
auto [e, q] = pq.top();
pq.pop();
q -= k;
if (q) pq.emplace(e, q);
}
};
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> DS(n);
for (auto& di : DS)
cin >> di;
vector<int> QS(n);
for (auto& qi : QS)
cin >> qi;
vector<int> CS(n);
for (auto& ci : CS)
cin >> ci;
vector<int> AS(n);
for (auto& ai : AS)
cin >> ai;
ll ans = 0;
VeniceSet st;
for (int i = 0; i < n; i++) {
st.add_element(CS[i], QS[i]);
while (DS[i]) {
auto [v, q] = st.best();
ans += min(q, (ll)DS[i]) * v;
st.pop_k(min(q, (ll)DS[i]));
DS[i] = max(DS[i]-q, 0ll);
}
st.update_all(AS[i]);
}
cout << ans << '\n';
}
// AC, greddy, data structures