## Algorithm

Problem Name: beecrowd | 2775

# Preparing Production

By João Gustavo Rogel de Oliveira, INATEL Brazil

Timelimit: 1

A car assembler allows users to create their own vehicle designs the way they want and still share that information with other users in order to create a diverse network of users. The process begins with the customer developing his own model through a software, soon after completion, the project data are stored and according to the availability of the assembler are being carried out.

But a failure to deliver parts to the automaker is delaying orders. It turns out that the parts are delivered in packages, labeled with a number, that should be ordered in an increasing order for the production to begin. The failure is that the packages are being delivered in a random manner. You should create a program in which given the order of delivery of the packages and the time that each of them takes to be changed of position, calculate the total time to organize the packages. It is known that for the purpose of organization within the company, packages should be changed from position only two to two and if they are side by side.

## Input

The input consists of several test cases, read up to EOF. For each case, the first value of the input is an integer N (1 <= N <= 1000) representing the number of packets, soon after there will be two lines with N integers each, with the numbers of the packets, in the order of delivery, and the time that the nth packet takes to be replaced, respectively.

It is guaranteed that the packet numbers for each test case form a permutation of integers from 1 to N, and that no packet takes more than one minute to move.

## Output

Your program should present, for each test case, a single integer representing the total time to organize the packages.

 Input Sample Output Sample 5 3 1 2 5 4 10 23 15 18 30 3 3 2 1 1 1 1 106 6

## Code Examples

### #1 Code Example with C++ Programming

```Code - C++ Programming```

``````
#include <stdio.h>
#include <algorithm>
using namespace std;
#define MAX 1123
#define fst first
#define snd second

typedef pair < int, int> ii;

int main(){
int N, i, j, cost;
ii v[MAX];
while(scanf("%d", &N) != EOF){
for(i = 0; i  <  N; i++){
scanf("%d", &v[i].fst);
}
for(i = 0; i  <  N; i++){
scanf("%d", &v[i].snd);
}
cost = 0;
for(i = 0; i  <  N; i++){
for(j = 1; j  <  N; j++){
if(v[j-1].fst > v[j].fst){
swap(v[j-1], v[j]);
cost += v[j-1].snd + v[j].snd;
}
}
}
printf("%d\n", cost);
}
return 0;
}
``````
Copy The Code &

Input

cmd
5 3 1 2 5 4 10 23 15 18 30 3 3 2 1 1 1 1

Output

cmd
106 6