Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <vector>
- //#include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const int maxn = 2e5 + 10;
- ll segment_tree[maxn * 3];
- void build(int L, int R, int pos) {
- if(L == R) {
- segment_tree[pos] = 0;
- }
- else {
- int mid = (L + R) / 2;
- build(L, mid, 2 * pos);
- build(mid + 1, R, 2 * pos + 1);
- segment_tree[pos] = max(segment_tree[2 * pos], segment_tree[2 * pos + 1]);
- }
- }
- // L R i L R j L R
- ll query(int L, int R, int pos, int i, int j) {
- if(i <= L and R <= j) {
- return segment_tree[pos];
- }
- if(R < i or j < L) {
- return 0;
- }
- int mid = (L + R) / 2;
- return max(query(L, mid, 2 * pos, i, j), query(mid + 1, R, 2 * pos + 1, i, j));
- }
- void update(int L, int R, int pos, int idx, ll new_val) {
- if(L == R) {
- segment_tree[pos] = new_val;
- return;
- }
- int mid = (L + R) / 2;
- if(idx <= mid) {
- update(L, mid, 2 * pos, idx, new_val);
- }
- else {
- update(mid + 1, R, 2 * pos + 1, idx, new_val);
- }
- segment_tree[pos] = max(segment_tree[2 * pos], segment_tree[2 * pos + 1]);
- }
- int main() {
- ios_base::sync_with_stdio(false);
- int n;
- cin >> n;
- vector<int> h(n), a(n);
- for(int i = 0; i < n; i++) {
- cin >> h[i];
- }
- for(int i = 0; i < n; i++) {
- cin >> a[i];
- }
- build(0, n + 1, 1);
- for(int i = 0; i < n; i++) {
- ll max_dp = query(0, n + 1, 1, 0, h[i]);
- update(0, n + 1, 1, h[i], max_dp + a[i]);
- }
- cout << query(0, n + 1, 1, 0, n + 1) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement