Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #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 position) {
- if(L == R) {
- segment_tree[position] = 0;
- }
- else {
- int middle = (L + R) / 2;
- build(L, middle, 2 * position);
- build(middle + 1, R, 2 * position + 1);
- segment_tree[position] = max(segment_tree[2 * position], segment_tree[2 * position + 1]);
- }
- }
- // L R i L R j L R
- ll query(int L, int R, int position, int i, int j) {
- if(i <= L and R <= j) {
- return segment_tree[position];
- }
- if(R < i or j < L) {
- return 0;
- }
- int middle = (L + R) / 2;
- return max(query(L, middle, 2 * position, i, j), query(middle + 1, R, 2 * position + 1, i, j));
- }
- void update(int L, int R, int position, int idx, ll new_value) {
- if(L == R) {
- segment_tree[position] = new_value;
- return;
- }
- int middle = (L + R) / 2;
- if(idx <= middle) {
- update(L, middle, 2 * position, idx, new_value);
- }
- else {
- update(middle + 1, R, 2 * position + 1, idx, new_value);
- }
- segment_tree[position] = max(segment_tree[2 * position], segment_tree[2 * position + 1]);
- }
- int main() {
- int n;
- cin >> n;
- vector<ll> heights(n), values(n), dp(n + 1);
- for(int i = 0; i < n; i++) {
- cin >> heights[i];
- }
- for(int i = 0; i < n; i++) {
- cin >> values[i];
- dp[i] = values[i];
- }
- ll res = 0;
- build(0, n, 1);
- for(int i = 0; i < n; i++) {
- ll max_dp = query(0, n, 1, 0, heights[i]);
- update(0, n, 1, heights[i], max_dp + values[i]);
- // for(int j = 0; j < i; j++) {
- // if(heights[j] < heights[i]) {
- // max_dp = max(max_dp, dp[j]);
- // }
- // }
- // dp[i] = max_dp + values[i];
- // res = max(res, dp[i]);
- }
- cout << query(0, n, 1, 0, n) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement