Language: C++

#include<iostream>
#include<string>
#include<algorithm>
#include<stack>
#include<math.h>
#include<map>
#include<unordered_map> 
#include<vector>
#include<queue>
#include<set>
#include<deque> 
#include<bitset>
#include<string>
#define N 40
#define K 300005
#define MOD 1e9+7
#define int long long 
#define fastio  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
 
int max(int a, int b){
    if(a>b)
        return a;
    return b;
}
int min(int a, int b){
    if(a<b)
        return a;
    return b;
}
 
int gcd(int a, int b) 
{ 
    if (b == 0) 
        return a; 
    return gcd(b, a % b);  
      
} 
//built assuming 2e5 elements
int minn[8000005];
int maxx[8000005];
int delta[8000005];
int lo[8000005];
int hi[8000005];

void init(int i, int a, int b){
	lo[i] = a;
	hi[i] = b;
	if(a == b)
		return;
	int mid = (lo[i] + hi[i])/2;
	init(2*i, a, mid);
	init(2*i + 1, mid + 1, b);
}

void prop(int i){
	delta[2*i] += delta[i];
	delta[2*i + 1] += delta[i];

	delta[i] = 0;
}

void update(int i){
	minn[i] = min(minn[2*i] + delta[2*i], minn[2*i+1] + delta[2*i+1]);
	maxx[i] = max(maxx[2*i] + delta[2*i], maxx[2*i + 1] + delta[2*i+1]);
}
void increment(int i, int a, int b, int val){
	if(b < lo[i] || hi[i] < b)
		return;
	if(a<= lo[i] && hi[i] <= b){
		delta[i] += val;
		return;
	}
	//partial covering case
	prop(i);
	increment(2*i, a, b, val);
	increment(2*i+1, a, b, val);
	update(i);
} 
pair<int, int> maxmin(int i, int a, int b){
	if(b < lo[i] || hi[i] < a)
		return {INT_MIN + 5, INT_MAX - 5};
	if(a <= lo[i] && hi[i] <= b){
		return {maxx[i] + delta[i], minn[i] + delta[i]};
	}
	//partial covering case
	pair<int, int> left, right;
	prop(i);
	left = maxmin(2*i, a, b);
	right = maxmin(2*i+1, a, b);
	update(i);
	return {max(left.first, right.first), min(left.second, right.second)};
}

int32_t main(){
	
	int n;
	cin>>n;
	init(1, 1, n);
	
	int h[n+1];
	int b[n + 1];
    for(int i = 1;i<=n;i++){
		cin>>h[i];
    }
    for(int i=1;i<=n;i++){
		cin>>b[i];
        //cout<<i<<" "<<b[i]<<endl;
    }
		
	increment(1, h[1], h[1], b[1]);
	for(int fl = 2; fl<=n; fl++){
		int nw = h[fl];
        //cout<<"precom: "<<fl<<" "<<maxmin(1, 2, 2).first <<endl;
		int max_end_before = maxmin(1, 1, nw -1).first;
        //cout<<fl<<" "<<max_end_before<<endl;
        //cout<<fl<<" "<<max_end_before<<endl;
        
		increment(1, h[fl], h[fl], max_end_before + b[fl]);
        //cout<<fl<<" "<<maxmin(1, h[fl], h[fl]).first<<endl;
        //cout<<fl<<" "<<h[fl]<<" "<<max_end_before + b[fl]<<endl;
        //cout<<nw -1<<" "<<h[fl]<<" "<<max_end_before + b[fl]<<endl;
	}
	int ans = maxmin(1, 1, n).first;
	
	
	cout<<ans<<endl;	
}




Compiler output:
a.cc: In function 'std::pair<long long int, long long int> maxmin(long long int, long long int, long long int)':
a.cc:82:25: error: 'INT_MIN' was not declared in this scope
   82 |                 return {INT_MIN + 5, INT_MAX - 5};
      |                         ^~~~~~~
a.cc:13:1: note: 'INT_MIN' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
   12 | #include<bitset>
  +++ |+#include <climits>
   13 | #include<string>
a.cc:82:38: error: 'INT_MAX' was not declared in this scope
   82 |                 return {INT_MIN + 5, INT_MAX - 5};
      |                                      ^~~~~~~
a.cc:82:38: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
a.cc:82:49: error: could not convert '{<expression error>, <expression error>}' from '<brace-enclosed initializer list>' to 'std::pair<long long int, long long int>'
   82 |                 return {INT_MIN + 5, INT_MAX - 5};
      |                                                 ^
      |                                                 |
      |                                                 <brace-enclosed initializer list>