Language: C++

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// 抽象化したセグメント木
template<class Monoid> struct SegTree {
    using Func = function<Monoid(Monoid, Monoid)>;
    const Func F;
    const Monoid UNITY;
    int SIZE_R;
    vector<Monoid> dat;

    SegTree(int n, const Func f, const Monoid &unity): F(f), UNITY(unity) { init(n); }
    void init(int n) {
        SIZE_R = 1;
        while (SIZE_R < n) SIZE_R *= 2;
        dat.assign(SIZE_R * 2, UNITY);
    }

    /* set, a is 0-indexed */
    void set(int a, const Monoid &v) { dat[a + SIZE_R] = v; }
    void build() {
        for (int k = SIZE_R - 1; k > 0; --k)
            dat[k] = F(dat[k*2], dat[k*2+1]);
    }

    /* update a, a is 0-indexed */
    void update(int a, const Monoid &v) {
        int k = a + SIZE_R;
        dat[k] = v;
        while (k >>= 1) dat[k] = F(dat[k*2], dat[k*2+1]);
    }

    /* get [a, b), a and b are 0-indexed */
    Monoid get(int a, int b) {
        Monoid vleft = UNITY, vright = UNITY;
        for (int left = a + SIZE_R, right = b + SIZE_R; left < right; left >>= 1, right >>= 1) {
            if (left & 1) vleft = F(vleft, dat[left++]);
            if (right & 1) vright = F(dat[--right], vright);
        }                                                                                                              
        return F(vleft, vright);
    }
    inline Monoid operator [] (int a) { return dat[a + SIZE_R]; }

    /* debug */
    void print() {
        for (int i = 0; i < SIZE_R; ++i) {
            cout << (*this)[i];
            if (i != SIZE_R-1) cout << ",";
        }
        cout << endl;
    }
};

int main() {
    int N; cin >> N;
    vector<long long> h(N), a(N);
    for (int i = 0; i < N; ++i) cin >> h[i];
    for (int i = 0; i < N; ++i) cin >> a[i];

    // セグメント木 (区間取得を max としたもの)
    SegTree<long long> dp(N+1,
                          [](long long a, long long b) { return max(a, b); },
                          0);

    // 更新
    long long res = 0;
    for (int i = 0; i < N; ++i) {
        // 値取得
        long long A = dp.get(0, h[i]);

        // 更新
        if (dp.get(h[i], h[i]+1) < A + a[i]) {
            dp.update(h[i], A + a[i]);
            res = max(res, A + a[i]);
        }
    }
    cout << res << endl;
}


Compiler output:
a.cc:8:18: error: 'function' does not name a type; did you mean 'union'?
    8 |     using Func = function<Monoid(Monoid, Monoid)>;
      |                  ^~~~~~~~
      |                  union
a.cc:9:11: error: 'Func' does not name a type
    9 |     const Func F;
      |           ^~~~
a.cc:14:26: error: 'Func' does not name a type
   14 |     SegTree(int n, const Func f, const Monoid &unity): F(f), UNITY(unity) { init(n); }
      |                          ^~~~
a.cc: In constructor 'SegTree<Monoid>::SegTree(int, int, const Monoid&)':
a.cc:14:56: error: class 'SegTree<Monoid>' does not have any field named 'F'
   14 |     SegTree(int n, const Func f, const Monoid &unity): F(f), UNITY(unity) { init(n); }
      |                                                        ^
a.cc: In function 'int main()':
a.cc:64:27: error: invalid user-defined conversion from 'main()::<lambda(long long int, long long int)>' to 'int' [-fpermissive]
   64 |                           [](long long a, long long b) { return max(a, b); },
      |                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:64:27: note: candidate is: 'constexpr main()::<lambda(long long int, long long int)>::operator long long int (*)(long long int, long long int)() const' (near match)
   64 |                           [](long long a, long long b) { return max(a, b); },
      |                           ^
a.cc:64:27: note:   no known conversion from 'long long int (*)(long long int, long long int)' to 'int'
a.cc:14:31: note:   initializing argument 2 of 'SegTree<Monoid>::SegTree(int, int, const Monoid&) [with Monoid = long long int]'
   14 |     SegTree(int n, const Func f, const Monoid &unity): F(f), UNITY(unity) { init(n); }
      |                    ~~~~~~~~~~~^
a.cc: In instantiation of 'Monoid SegTree<Monoid>::get(int, int) [with Monoid = long long int]':
a.cc:71:29:   required from here
   71 |         long long A = dp.get(0, h[i]);
      |                       ~~~~~~^~~~~~~~~
a.cc:39:36: error: 'F' was not declared in this scope
   39 |             if (left & 1) vleft = F(vleft, dat[left++]);
      |                                   ~^~~~~~~~~~~~~~~~~~~~
a.cc:40:38: error: 'F' was not declared in this scope
   40 |             if (right & 1) vright = F(dat[--right], vright);
      |                                     ~^~~~~~~~~~~~~~~~~~~~~~
a.cc:42:17: error: 'F' was not declared in this scope
   42 |         return F(vleft, vright);
      |                ~^~~~~~~~~~~~~~~
a.cc: In instantiation of 'void SegTree<Monoid>::update(int, const Monoid&) [with Monoid = long long int]':
a.cc:75:22:   required from here
   75 |             dp.update(h[i], A + a[i]);
      |             ~~~~~~~~~^~~~~~~~~~~~~~~~
a.cc:32:35: error: 'F' was not declared in this scope
   32 |         while (k >>= 1) dat[k] = F(dat[k*2], dat[k*2+1]);
      |                                  ~^~~~~~~~~~~~~~~~~~~~~~