Language: C++

#include<iostream>
#include<string>
#include<map>
#include<unordered_map>
#include<algorithm>
#include<vector>
using namespace std;
#define ll long long
vector<ll> h,a;
ll n;
map<pair<int, int>, int> dp;
ll f(ll cur, ll last){
    if(cur==n) return 0;
    if(dp.find(make_pair(cur, last))!=dp.end()) return dp[make_pair(cur, last)];
    if(h[cur] > last) return dp[make_pair(cur, last)] = max(a[cur] + f(cur+1, h[cur]), f(cur+1, last));
    else return dp[make_pair(cur, last)] = f(cur+1, last);
}
ll max(ll a, ll b){
    return (a>b)? a:b;
}

ll dp2[200005];

ll g(ll cur){
    if(cur==0){
        return a[0];
    }
    if(dp2[cur]!=-1) return dp2[cur];
    ll Max = a[cur];
    for(ll i=0;i<cur;i++){
        if(h[i]< h[cur]){
            Max = max(Max, a[cur]+g(i));
        }
    }
    return dp2[cur]=Max;
}
int main(){
    // int n;
    memset(dp2, -1, sizeof(dp2));
    cin>>n;
    h.assign(n,0);
    a.assign(n,0);
    for(ll i=0;i<n;i++) cin>>h[i];
    for(ll i=0;i<n;i++) cin>>a[i];
    cout<<g(n-1)<<endl;
    return 0;
}
/*
    )((())))(()())
    (()()((())
    ()(())()
    (())(()()

*/
/*
#include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
// #include <ext/pb_ds/assoc_container.hpp> 
// #include <ext/pb_ds/tree_policy.hpp>
#define ll long long
using namespace std;
// using namespace __gnu_pbds;

// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
vector<vector<int> > children;
vector<bool> visited;
vector<vector<int> > a;
void dfs(int cur){
    visited[cur] = true;
    for(int i=0;i<a[cur].size();i++){
        if(!visited[a[cur][i]]){
            children[cur].push_back(a[cur][i]);
            dfs( a[cur][i]);
        }
    }
}

int M = 1000000007;

vector<vector<int> > dp;

int dfs2(int cur, bool b){
    if(children[cur].size()==0){
        return 1;
    }
    if(dp[cur][(int)b] != -1) return dp[cur][(int)b];
    if(b){
        int sum=1;
        for(int i=0;i<children[cur].size();i++){
            sum*=dfs2(children[cur][i], false);
            sum%=M;
        }
        return dp[cur][(int)b] =sum%M;
    }
    else{
        int sum=1;
        for(int i=0;i<children[cur].size();i++){
            sum*=(dfs2(children[cur][i], false) + dfs2(children[cur][i], true));
            sum%=M;
        }
        return dp[cur][(int)b] =sum%M; // 
    }
}

int main(){
    // memset(dp, sizeof(dp), -1);
    vector<int> x(2,-1);
    dp.assign(100005, x);
    int n;
    cin>>n;
    
    vector<int> c;
    // vector<vector<int> > a(n,c);
    a.assign(n, c);
    children.assign(n,c);
    visited.assign(n, false);
    for(int i=0;i<n-1;i++){
        int u,v;
        cin>>u>>v;
        a[u-1].push_back(v-1);
        a[v-1].push_back(u-1);
    }
    dfs( 0);
    
    // for(int i=0;i<n;i++){
    //     for(int j=0;j<children[i].size();j++) cout<<children[i][j]<<" ";
    //     cout<< endl;
    // }
    cout<<dfs2(0, false) + dfs2(0,true)<<endl;
    return 0;
}
/*lllllll
    6 
    5 2 6 4 3 8
    4
    6 3 9 1
    100
    20 10 5
*/


Compiler output:
a.cc: In function 'int main()':
a.cc:39:5: error: 'memset' was not declared in this scope
   39 |     memset(dp2, -1, sizeof(dp2));
      |     ^~~~~~
a.cc:7:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    6 | #include<vector>
  +++ |+#include <cstring>
    7 | using namespace std;