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: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using BitArray = System.Collections.BitArray;
using BigInteger = System.Numerics.BigInteger;

namespace AtCoderProject
{
    public class Program
    {
        public object Calc()
        {
            var N = consoleReader.Int;
            var h = consoleReader.Split.Int;
            var a = consoleReader.Split.Int;

            var hMax = h.Max();
            long allMax = 0;

            var beautyPoints = new SegmentTree(hMax + 1);

            for (int i = 0; i < N; i++)
            {
                var beauty = a[i];
                var height = h[i];

                long newBeauty = beautyPoints.Query(0, height, 0, 0, hMax) + beauty;

                beautyPoints.Update(height, newBeauty);
                allMax = Math.Max(allMax, newBeauty);
            }
            return allMax;
        }

        class SegmentTree
        {
            // この辺は場合によって変える
            private long defaultValue = 0;
            private long Which(long left, long right)
            {
                return Math.Max(left, right);
            }


            private int origLength;
            private int rootLength;
            private long[] tree;

            public SegmentTree(long[] initArray) : this(initArray.Length)
            {
                for (int i = 0; i < initArray.Length; i++)
                    Update(i, initArray[i]);
            }
            public SegmentTree(int size)
            {
                origLength = size;
                for (rootLength = 1; rootLength <= size; rootLength <<= 1) { }
                tree = Enumerable.Repeat(defaultValue, 2 * rootLength - 1).ToArray();
            }

            public void Update(int index, long value)
            {
                index += rootLength - 1;
                tree[index] = value;
                while (index > 0)
                {
                    index = (index - 1) >> 1;
                    tree[index] = Which(tree[index * 2 + 1], tree[index * 2 + 2]);
                }
            }


            public long Query(int fromInclusive, int toExclusive) => Query(fromInclusive, toExclusive, 0, 0, origLength);
            public long Query(int fromInclusive, int toExclusive, int nodeIndex, int nodeStart, int nodeEnd)
            {
                if (nodeEnd <= fromInclusive || toExclusive <= nodeStart) return defaultValue;
                if (fromInclusive <= nodeStart && nodeEnd <= toExclusive) return tree[nodeIndex];

                var left = Query(fromInclusive, toExclusive, nodeIndex * 2 + 1, nodeStart, (nodeStart + nodeEnd) / 2);
                var right = Query(fromInclusive, toExclusive, nodeIndex * 2 + 1, (nodeStart + nodeEnd) / 2, nodeEnd);
                return Which(left, right);
            }
        }

        #region いつもの
        private readonly ConsoleReader consoleReader;
        public Program(ConsoleReader consoleReader) { this.consoleReader = consoleReader; }
        static void Main() => Console.WriteLine(new Program(new ConsoleReader()).Calc()); static string AllLines<T>(IEnumerable<T> source) => string.Join("\n", source);
        #endregion
    }

#pragma warning disable
    public class ConsoleReader { public class SplitLine { string[] splited; public SplitLine(string line) { splited = line.Split(); } public string[] String => splited.ToArray(); public int[] Int => splited.Select(int.Parse).ToArray(); public long[] Long => splited.Select(long.Parse).ToArray(); public ulong[] ULong => splited.Select(ulong.Parse).ToArray(); public double[] Double => splited.Select(double.Parse).ToArray(); } public class RepeatLine { ConsoleReader reader; int num; public RepeatLine(ConsoleReader reader, int num) { this.reader = reader; this.num = num; } public IEnumerable<string> String { get { for (int i = 0; i < num; i++) yield return reader.String; } } public IEnumerable<int> Int { get { for (int i = 0; i < num; i++) yield return reader.Int; } } public IEnumerable<long> Long { get { for (int i = 0; i < num; i++) yield return reader.Long; } } public IEnumerable<string[]> SplitString { get { for (int i = 0; i < num; i++) yield return reader.Split.String; } } public IEnumerable<int[]> SplitInt { get { for (int i = 0; i < num; i++) yield return reader.Split.Int; } } } public virtual string String => Console.ReadLine(); public int Int => int.Parse(String); public long Long => long.Parse(String); public ulong ULong => ulong.Parse(String); public double Double => double.Parse(String); public SplitLine Split => new SplitLine(String); public RepeatLine Repeat(int count) => new RepeatLine(this, count); }
}