Problem:
Score : 100 points

Problem Statement

There are N flowers arranged in a row.
For each i (1 \leq i \leq N), the height and the beauty of the i-th flower from the left is h_i and a_i, respectively.
Here, h_1, h_2, \ldots, h_N are all distinct.

Taro is pulling out some flowers so that the following condition is met:

The heights of the remaining flowers are monotonically increasing from left to right.

Find the maximum possible sum of the beauties of the remaining flowers.

Constraints

All values in input are integers.

1 \leq N \leq 2 × 10^5

1 \leq h_i \leq N

h_1, h_2, \ldots, h_N are all distinct.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 \ldots h_N
a_1 a_2 \ldots a_N

Output

Print the maximum possible sum of the beauties of the remaining flowers.

Sample Input 1

4
3 1 4 2
10 20 30 40

Sample Output 1

60

We should keep the second and fourth flowers from the left.
Then, the heights would be 1, 2 from left to right, which is monotonically increasing, and the sum of the beauties would be 20 + 40 = 60.

Sample Input 2

1
1
10

Sample Output 2

10

The condition is met already at the beginning.

Sample Input 3

5
1 2 3 4 5
1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

5000000000

The answer may not fit into a 32-bit integer type.

Sample Input 4

9
4 2 5 8 3 6 1 7 9
6 8 8 4 6 3 5 7 5

Sample Output 4

31

We should keep the second, third, sixth, eighth and ninth flowers from the left.

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        QFlowers solver = new QFlowers();
        solver.solve(1, in, out);
        out.close();
    }

    static class QFlowers {
        long[] tree = new long[(int) (1e6)];

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int N = in.nextInt();
            long[] dp = new long[N];
            // dp[i] max such that i is the max height flower
            int[] h = new int[N];
            int[] a = new int[N];
            int[] ind_h = new int[N];
            for (int i = 0; i < N; i++) {
                h[i] = in.nextInt() - 1;
                ind_h[h[i]] = i;
            }
            a = in.nextIntArray(N);
            for (int i = 0; i < N; i++) {
                dp[h[i]] = a[i];
                // maximum value for dp[j] for j in the interval 0 to h[i]-1
                dp[h[i]] += query(0, N - 1, 0, h[i] - 1, 1);
                update(0, N - 1, dp[h[i]], h[i], 1);
            }
            long ans = 0;
            for (int i = 0; i < N; i++) {
                ans = Math.max(ans, dp[i]);
            }
            out.print(ans);
        }

        void update(int st, int en, long val, int idx, int TN) {
            if (st == en) {
                tree[TN] = val;
                return;
            }
            int mid = (st + en) / 2;
            if (idx > mid) {
                update(mid + 1, en, val, idx, 2 * TN + 1);
            } else {
                update(st, mid, val, idx, 2 * TN);
            }
            tree[TN] = Math.max(tree[2 * TN], tree[2 * TN + 1]);
        }

        long query(int st, int en, int ql, int qr, int TN) {
            if (st > qr || en < ql) {
                return 0;
            } else if (st >= ql && en <= qr) {
                return tree[TN];
            } else {
                int mid = (st + en) / 2;
                long a1 = query(st, mid, ql, qr, 2 * TN);
                long a2 = query(mid + 1, en, ql, qr, 2 * TN + 1);
                return Math.max(a1, a2);
            }
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public int[] nextIntArray(int size) {
            int[] array = new int[size];
            for (int i = 0; i < size; i++)
                array[i] = nextInt();
            return array;
        }

    }
}