import java.io.*; import java.util.*; public class Solution { static final StdIn in = new StdIn(); static final PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { int n=in.nextInt(); long[] p = in.readLongArray(n); int[] x = in.readIntArray(n); TreeMap map = new TreeMap(); for(int i=0; i() { public int compare(Pair a, Pair b) { return a.a==a.b?-(a.b-b.b):a.a-b.a; } }); TreeSet uncovered = new TreeSet(); for(int i=0; i ranges2 = new ArrayList(), posAns = new ArrayList(); for(int i=0, lastEnd=0; iranges1[i].b) break; map.remove(a); } } else { while(true) { Integer a=uncovered.ceiling(ranges1[i].a); if(a==null||a>ranges1[i].b) break; uncovered.remove(a); } ranges2.add(ranges1[i]); } lastEnd=Math.max(ranges1[i].b, lastEnd); } for(int i=0; i pmap = new HashMap(); for(Map.Entry entry : map.entrySet()) { pref+=entry.getValue(); pmap.put(entry.getKey(), pref); } for(Pair pi : posAns) max=Math.max(pmap.getOrDefault(pi.b, 0L)-pmap.getOrDefault(pi.a, 0L), max); for(int i : uncovered) max+=map.get(i); out.println(max); out.close(); } static class Pair { int a, b; Pair(int a, int b) { this.a=a; this.b=b; } } interface Input { public String next(); public String nextLine(); public int nextInt(); public long nextLong(); public double nextDouble(); } static class StdIn implements Input { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public StdIn() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public StdIn(InputStream in) { try{ din = new DataInputStream(in); } catch(Exception e) { throw new RuntimeException(); } buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String next() { int c; while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r')); StringBuilder s = new StringBuilder(); while (c != -1) { if (c == ' ' || c == '\n'||c=='\r') break; s.append((char)c); c=read(); } return s.toString(); } public String nextLine() { int c; while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r')); StringBuilder s = new StringBuilder(); while (c != -1) { if (c == '\n'||c=='\r') break; s.append((char)c); c = read(); } return s.toString(); } public int nextInt() { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do ret = ret * 10 + c - '0'; while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public int[] readIntArray(int n) { int[] ar = new int[n]; for(int i=0; i= '0' && c <= '9'); if (neg) return -ret; return ret; } public long[] readLongArray(int n) { long[] ar = new long[n]; for(int i=0; i= '0' && c <= '9'); if (c == '.') while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10); if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() { try{ if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } catch(IOException e) { throw new RuntimeException(); } } public void close() throws IOException { if (din == null) return; din.close(); } } }