This page is still under construction.

Parts of this page are still being built. What you see may change.

Junseo the Librarian King

Time limit2sMemory limit512 MB

Summary
Given book numbers and weights, move the lightest total weight of books so the numbers end up in non-decreasing order.
Level

Medium5 of 10

Topics
Dynamic programming, Sorting, Greedy, Implementation
Solved
No attempts yet

Problem

Junseo wants to be the best librarian in the world. Library after library turned him down, but he passed the librarian hiring round at ANSI (Ajou Nerd Standards Institution), which owns the best library in the world.

Every new ANSI librarian goes through a training course that checks the basics of the job. The hardest part of it is reshelving. Books on a shelf must sit in non-decreasing order of book number so that visitors can find them, but Yongjae D. Ash, called the nerd king, sits in the library all day and pushes every book he has read into whatever slot he likes. It is a nerds' library, so the books are heavy too, and Junseo now has sore muscles.

Find the smallest labor needed to sort the shelf. Labor is the total force spent moving books, and moving one book takes force equal to its weight no matter how far it travels. The slot where a pulled book goes back always has enough room, and the job is finished as soon as the book numbers are in order.

Input

The first line contains the number of books NN (1≤N≤50001 \le N \le 5000) that Junseo has to sort.

The second line contains the NN book numbers in their current shelf order.

The third line contains the NN weights in the same order.

Every book number is a real number greater than 00 and less than 10001000, and every weight is an integer from 11 to 1000010000.

Output

Print the minimum labor needed to sort the shelf on one line.

Hint

In the first example, moving the third book to the front costs 66, which is the minimum.

In the second example, moving the first, third, fifth and eighth books into their right slots costs 6+5+17+41=696 + 5 + 17 + 41 = 69, which is the minimum.

Examples4

  1. Example 1

    Input
    3
    802.11 813.1 107
    4 5 6
    
    Expected output
    6
    
  2. Example 2

    Input
    9
    813.8 812 816 813 811 813 813.6 801.9 880.1
    6 20 5 8 17 20 12 41 6
    
    Expected output
    69
    
  3. Example 3

    Input
    1
    500.5
    7
    
    Expected output
    0
    
  4. Example 4

    Input
    5
    1 2.5 2.5 10 999.999
    5 5 5 5 5
    
    Expected output
    0