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 C# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.Linq;
using System.Collections.Generic;

namespace AtCoder.DP
{
    public class SegmentTree<T>
    {
        private readonly T ex;

        private readonly int n;

        private readonly int offset;

        private readonly T[] node;

        private readonly Func<T, T, T> func;

        public T this[int index] => node[offset + index];

        public SegmentTree(int m, Func<T, T, T> func, T ex)
        {
            this.func = func;
            this.ex = ex;

            n = 1;
            while (n < m)
                n *= 2;
            offset = n - 1;
            node = new T[n * 2 - 1];
            for (int i = 0; i < node.Length; i++)
                node[i] = ex;
        }

        public SegmentTree(int n, Func<T, T, T> operation, T init, T ex) : this(n, operation, ex)
        {
            for (int i = offset; i < offset + n; i++)
                node[i] = init;
        }

        public SegmentTree(T[] values, Func<T, T, T> operation, T ex) : this(values.Length, operation, ex)
        {
            Array.Copy(values, 0, node, offset, values.Length);
            UpdateTree();
        }

        public void Assign(int i, T value)
            => node[offset + i] = value;

        public void AssignAndUpdate(int i, T value)
        {
            i += offset;
            node[i] = value;

            while (i > 0)
            {
                i = (i - 1) / 2;
                node[i] = func(node[2 * i + 1], node[2 * i + 2]);
            }
        }

        public void UpdateTree()
        {
            for (int i = offset - 1; i >= 0; i--)
                node[i] = func(node[2 * i + 1], node[2 * i + 2]);
        }

        public T Query(int head, int foot)
            => Query(head, foot, 0, 0, n);

        private T Query(int head, int foot, int k, int left, int right)
        {
            T leftValue = ex;
            T rightValue = ex;
            int d = offset;

            while (head < foot)
            {
                if ((head & 1) == 1)
                    leftValue = func(leftValue, node[d + head++]);
                if ((foot & 1) == 0)
                    rightValue = func(node[d + foot--], rightValue);
                head >>= 1;
                foot >>= 1;
                d >>= 1;
            }
            return func(func(leftValue, node[d + head]), rightValue);
        }
    }

    class EDPC_Q
    {
        public static void Main()
        {
            var n = int.Parse(Console.ReadLine());
            var h = Console.ReadLine().Split(' ').Select(i => int.Parse(i)).ToArray();
            var a = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray();

            // i本目までの最大高さがjである場合についてdpを行う
            var dp = new long[n + 1];
            var segmentTree = new SegmentTree<long>(n, (long x, long y) => Math.Max(x, y), 0, 0);

            for (int i = 0; i < n; i++)
            {
                var max = segmentTree.Query(0, h[i] - 1);
                segmentTree.AssignAndUpdate(h[i] - 1, max + a[i]);
            }
            Console.WriteLine(segmentTree.Query(0, n));
        }
    }
}