#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
int n; cin >> n;
vector<pair<int, int>> a(n);
for (int i = 0; i < n; i++){
int c; cin >> c;
cin >> a[i].first >> a[i].second;
}
sort(a.begin(), a.end());
int m; cin >> m;
vector<pair<int, int>> b(m);
for (int i = 0; i < m; i++){
int c; cin >> c;
cin >> b[i].first >> b[i].second;
}
vector<vector<vector<int>>> dp(n + 1, vector<vector<int>> (m + 1, vector<int> (2, -1e18)));
for (int i = 0; i < n; i++){
dp[i][0][0] = 0;
if (a[i].first >= b[0].first){
dp[i][0][1] = b[0].second - a[i].second;
}
}
for (int j = 1; j < m; j++){
for (int i = 0; i < n; i++){
for (int l = 0; l <= i; l++){
if (dp[l][j - 1][0] != -1e18) dp[i][j][0] = max(dp[i][j][0], dp[l][j - 1][0]);
if (dp[l][j - 1][1] != -1e18) dp[i][j][0] = max(dp[i][j][0], dp[l][j - 1][1]);
if (a[i].first >= b[j].first) {
if (dp[l][j - 1][0] != -1e18) dp[i][j][1] = max(dp[i][j][1], dp[l][j - 1][0] + b[j].second - a[i].second);
if (dp[l][j - 1][1] != -1e18 && l < i) dp[i][j][1] = max(dp[i][j][1], dp[l][j - 1][1] + b[j].second - a[i].second);
}
}
}
}
int ans = 0;
for (int i = 0; i < n; i++){
ans = max(ans, dp[i][m - 1][0]);
ans = max(ans, dp[i][m - 1][1]);
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |