#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lld double
#define int ll
#define usaco(fname) freopen(#fname ".in","r",stdin);freopen(#fname ".out","w",stdout);
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
// const ll INF = 1e18;
const int INF = 1e18;
const int mod = 1e9+7;
const lld PI = acos(-1.0);
int di[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dj[] = {0, 0, 1, -1, 1, -1, 1, -1};
#define debug(x) cout << #x << ": " << x << endl;
#define add(a, b) a += b, a %= mod
#define mul(a, b) ((a % mod) * (b % mod)) % mod
#define all(x) x.begin(),x.end()
const int N = 1e5+1;
#define info tuple<int, int, int, int>
void solve() {
int n;cin>>n;
vector<info> a;
for (int i = 0; i < n; i++) {
int c,f,v;cin>>c>>f>>v;
// value, freq, cost
a.push_back({f, 0, c, v});
}
int m;cin>>m;
for (int i = 0; i < m; i++) {
int c,f,v;cin>>c>>f>>v;
a.push_back({f, 1, c, v});
}
vector<int> dp(N, -INF);
dp[0] = 0;
sort(all(a), [&](auto x, auto y) {
auto [f1, t1, c1, v1] = x;
auto [f2, t2, c2, v2] = y;
if (f1 == f2) return v1 < v2;
return f1 > f2;
});
// cout << dp << endl;
// for (auto [x, y, z, k] : a) cout << x << " " << y << " " << z << " " << k << endl;
int ans = 0;
for (int i = 0; i < n+m; i++) {
auto [x, k, z, y] = a[i];
if (k == 0) {
// buy
for (int j = N-1; j >= 0; j--) {
if (j-z >= 0) dp[j] = max(dp[j], dp[j - z] - y);
}
} else {
// sell
for (int j = 0; j < N; j++) {
if (j+z < N)dp[j] = max(dp[j], dp[j + z] + y);
}
}
}
// cout << dp << endl;
for (int i = 0; i < N; i++) ans = max(ans, dp[i]);
cout << ans << endl;
}
int32_t main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int t=1;
while(t--) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
7 ms |
1112 KB |
Output is correct |
4 |
Correct |
12 ms |
1272 KB |
Output is correct |
5 |
Correct |
110 ms |
1372 KB |
Output is correct |
6 |
Correct |
109 ms |
1372 KB |
Output is correct |
7 |
Correct |
114 ms |
1368 KB |
Output is correct |
8 |
Correct |
113 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1224 KB |
Output is correct |
3 |
Correct |
9 ms |
1116 KB |
Output is correct |
4 |
Correct |
8 ms |
1264 KB |
Output is correct |
5 |
Correct |
52 ms |
1116 KB |
Output is correct |
6 |
Correct |
54 ms |
1116 KB |
Output is correct |
7 |
Correct |
114 ms |
1372 KB |
Output is correct |
8 |
Correct |
125 ms |
1372 KB |
Output is correct |
9 |
Correct |
109 ms |
1368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1116 KB |
Output is correct |
2 |
Correct |
2 ms |
1116 KB |
Output is correct |
3 |
Correct |
10 ms |
1268 KB |
Output is correct |
4 |
Correct |
10 ms |
1276 KB |
Output is correct |
5 |
Correct |
21 ms |
1116 KB |
Output is correct |
6 |
Correct |
21 ms |
1116 KB |
Output is correct |
7 |
Correct |
28 ms |
1116 KB |
Output is correct |
8 |
Correct |
36 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
86 ms |
1372 KB |
Output is correct |
4 |
Incorrect |
107 ms |
1372 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
9 ms |
1116 KB |
Output is correct |
3 |
Correct |
46 ms |
1116 KB |
Output is correct |
4 |
Correct |
124 ms |
1372 KB |
Output is correct |
5 |
Correct |
223 ms |
1548 KB |
Output is correct |
6 |
Correct |
232 ms |
1368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
7 ms |
1112 KB |
Output is correct |
4 |
Correct |
12 ms |
1272 KB |
Output is correct |
5 |
Correct |
110 ms |
1372 KB |
Output is correct |
6 |
Correct |
109 ms |
1372 KB |
Output is correct |
7 |
Correct |
114 ms |
1368 KB |
Output is correct |
8 |
Correct |
113 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1224 KB |
Output is correct |
11 |
Correct |
9 ms |
1116 KB |
Output is correct |
12 |
Correct |
8 ms |
1264 KB |
Output is correct |
13 |
Correct |
52 ms |
1116 KB |
Output is correct |
14 |
Correct |
54 ms |
1116 KB |
Output is correct |
15 |
Correct |
114 ms |
1372 KB |
Output is correct |
16 |
Correct |
125 ms |
1372 KB |
Output is correct |
17 |
Correct |
109 ms |
1368 KB |
Output is correct |
18 |
Correct |
2 ms |
1116 KB |
Output is correct |
19 |
Correct |
2 ms |
1116 KB |
Output is correct |
20 |
Correct |
10 ms |
1268 KB |
Output is correct |
21 |
Correct |
10 ms |
1276 KB |
Output is correct |
22 |
Correct |
21 ms |
1116 KB |
Output is correct |
23 |
Correct |
21 ms |
1116 KB |
Output is correct |
24 |
Correct |
28 ms |
1116 KB |
Output is correct |
25 |
Correct |
36 ms |
1116 KB |
Output is correct |
26 |
Correct |
2 ms |
1116 KB |
Output is correct |
27 |
Correct |
1 ms |
1116 KB |
Output is correct |
28 |
Correct |
86 ms |
1372 KB |
Output is correct |
29 |
Incorrect |
107 ms |
1372 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |