Description
The old tracer tribe recently elected Dalong as the new chief of the tribe. As the saying goes, the first thing Dalong will do after taking office is to improve the communication among the villagers. The tracer tribe has a vast territory. People usually need to communicate with each other by using pigeons to transmit information. This method compromises the timeliness of information. Dalong, who graduated from "Information Whampoa", decided to build a communication system for the villagers through his advanced communication knowledge, so that everyone can communicate conveniently and efficiently. Before building a communication system, let's take a look at the topography of the tracer tribe: There are n households in the tracer tribe, and interestingly, the N households are in a straight line.
Dalong's communication system needs to select one of the N households to establish a base station, and then establish a separate dedicated Wired connection with each n-1 households. For security considerations, each wire segment transmits data only to a non-base station user. (For example, if there are three sites A, B, and C, B is in the middle, and the base station is in a, the AB and AC connections need to be established, and AB is not part of the AC.) Now, dalong has already equipped receiving devices for N households. He needs to select a suitable home to build base stations to minimize the total length of the laid wires. However, some families do not want to build base stations in their own homes. Your task is to help him calculate the minimum number of wires required.
Input
Test multiple groups of data
For each group of data, the first line of the input is a positive integer N (1 <= n <= 100000), indicating that the tracer tribe has n families.
The second line of the input is n positive integers A (0 <= A <= 1000000). The number of I represents the coordinates of the I family.
The third line of the input is n integers B (0 <= B <= 1). B = 0 indicates that base stations cannot be built in the I-th family, B = 1 indicates that the first home can be used to build a base station. Data ensures that at least one home can build a base station, and the last input is a number of zeros, indicating that the input is over and this group of data is not processed.
Output
For each set of data, the total length of the cables that Dalong needs to lay at least is output. The answer must be less than 2 ^ 31.
Sample Input
41 2 3 41 1 1 151 2 3 5 41 1 0 1 10
Sample output
47
The method used is stupid.
The first brute-force method was used, and it timed out as expected.
Then, improvements were made.
Sort the coordinates first. We can find that each time we reach the next coordinate, we can quickly obtain the answer based on the distance obtained from the previous coordinate. Greatly optimized the speed.
I don't know if there are any better methods.
Import java. Io. bufferedinputstream;
Import java. util. arrays;
Import java. util. collections;
Public class brilliant student {
Static int OPT [];
Static int [] bool = new int [1000000];
Public static void main (string [] ARGs ){
Scanner S = new second (New bufferedinputstream (system. In, 2048 ));
Int N;
While (n = S. nextint ())! = 0 ){
Int [] arr1 = new int [N];
Opt = new int [N];
For (INT I = 0; I <n; I ++)
Arr1 [I] = S. nextint ();
For (INT I = 0; I <n; I ++)
Bool [arr1 [I] = S. nextint ();
Arrays. Sort (arr1 );
Int temp = integer. max_value; // minimum record distance
For (INT I = 1; I <n; I ++)
OPT [0] + = (arr1 [I]-arr1 [0]);
For (Int J = 1; j <n; j ++ ){
Int T = arr1 [J]-arr1 [J-1];
OPT [J] = OPT [J-1] + (J-1) * (arr1 [J]-arr1 [J-1])-(n-j-1) * t;
}
For (INT I = 0; I <n; I ++ ){
If (bool [arr1 [I] = 0)
Continue;
If (OPT [I] <temp)
Temp = OPT [I];
}
System. Out. println (temp );
}
}
}