Untitled

mail@pastecode.io avatar
unknown
c_cpp
7 months ago
1.1 kB
2
Indexable
Never
#include<bits/stdc++.h>
using namespace std;
using l = long;
l fun(vector<int>prices, vector<int>profit){
    int n = prices.size();
    l ans = 0;
    vector<int>v(n, 0);
    v.push_back(0);
    for(int i = 1; i < n; i++){
        if(prices[i] > prices[v.back()])
            v.push_back(i);
        else if(prices[i] == prices[v.back()] && profit[i] > profit[v.back()])
            v.back() = i;
    }
    int m = v.size();
    vector<int>pos(m, 0);
    int pos_max = profit[v.back()];
    for(int i = m-1; i >= 0; i--){
        pos_max = max(pos_max, profit[v[i]]);
        pos[i] = pos_max;
    }
    for(int i = 0; i < m; i++){
        for(int j = i+1; j+1 < m; j++){
            ans = max(ans, (l)(profit[v[i]]+profit[v[j]]+pos[j+1]));
        }
    }
    return ans;
}
int main(){
    int n;
    cin >> n;
    vector<int>prices(n, 0);
    vector<int>profits(n, 0);
    for(int i = 0; i < n; i++){
        cin >> prices[i];
    }
    for(int i = 0; i < n; i++){
        cin >> profits[i];
    }
    cout << fun(prices, profits) << '\n';
    return 0;
}