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) + 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, rootLength);
            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 + 2, (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); }
}

Measured memory_bytes: 44228