Untitled
unknown
c_cpp
a year ago
2.3 kB
3
Indexable
Never
#include "bits/stdc++.h" using namespace std; #ifdef LOCAL #include "debug.h" #else #define debug(x...) #endif constexpr int maxN = 40000; constexpr int maxP = 100000; int n, m, p; vector<long long> cost(2 * maxN); vector<pair<int, int>> edges; vector<int> graph[2 * maxN]; vector<int> nxt(2 * maxP, -1); vector<long long> solved(2 * maxP, -1); long long solve(int num) { if (solved[num] != -1) { return solved[num]; } //debug(edges[num].second, cost[edges[num].second]); solved[num] = cost[edges[num].second]; if (nxt[num] != -1) { solved[num] = max(solve(nxt[num]), solve(nxt[num] ^ 1) + cost[edges[num].second]); } else { for (int i = graph[edges[num].second].size() - 1; i >= 0; i--) { int curp = graph[edges[num].second][i]; if (edges[curp].second <= edges[num].first) { break; } solved[num] = max(solved[num], solve(curp) + cost[edges[num].second]); } } return solved[num]; } signed main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m >> p; long long ans = 0; for (int i = 0; i < n; i++) { cin >> cost[i]; ans = max(ans, cost[i]); } for (int i = n; i < n + m; i++) { cin >> cost[i]; ans = max(ans, cost[i]); } for (int i = 0; i < p; i++) { int u, v; cin >> u >> v; u--; v--; v += n; edges.emplace_back(u, v); graph[u].push_back(2 * i); edges.emplace_back(v, u); graph[v].push_back(2 * i + 1); } for (int i = 0; i < n + m; i++) { sort(graph[i].begin(), graph[i].end(), [&](int lhs, int rhs) { return edges[lhs].second < edges[rhs].second; }); } // nxt precalc for (int i = 0; i < n + m; i++) { for (int j = 0; j < (int)graph[i].size() - 1; j++) { nxt[graph[i][j] ^ 1] = graph[i][j + 1] ^ 1; } } // for (int i = 0; i < 2 * p; i++) { // debug(edges[i], (nxt[i] == -1 ? make_pair(-1, -1) : edges[nxt[i]])); // } for (int i = 0; i < 2 * p; i++) { ans = max(solve(i) + cost[edges[i].first], ans); } cout << ans; }