CF1651C.Fault-tolerant Network
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There is a classroom with two rows of computers. There are n computers in each row and each computer has its own grade. Computers in the first row has grades a1,a2,…,an and in the second row — b1,b2,…,bn .
Initially, all pairs of neighboring computers in each row are connected by wire (pairs (i,i+1) for all 1≤i<n ), so two rows form two independent computer networks.
Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the i -th computer from the first row and the j -th computer from the second row costs ∣ai−bj∣ .
You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts.
That is the minimum total cost to make a fault-tolerant network?
输入格式
The first line contains a single integer t ( 1≤t≤104 ) — the number of test cases. Next t cases follow.
The first line of each test case contains the single integer n ( 3≤n≤2⋅105 ) — the number of computers in each row.
The second line contains n integers a1,a2,…,an ( 1≤ai≤109 ) — the grades of computers in the first row.
The third line contains n integers b1,b2,…,bn ( 1≤bi≤109 ) — the grades of computers in the second row.
It's guaranteed that the total sum of n doesn't exceed 2⋅105 .
输出格式
For each test case, print a single integer — the minimum total cost to make a fault-tolerant network.
输入输出样例
输入#1
2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000
输出#1
31 1999999998
说明/提示
In the first test case, it's optimal to connect four pairs of computers:
- computer 1 from the first row with computer 2 from the second row: cost ∣1−4∣=3 ;
- computer 3 from the first row with computer 2 from the second row: cost ∣1−4∣=3 ;
- computer 2 from the first row with computer 1 from the second row: cost ∣10−20∣=10 ;
- computer 2 from the first row with computer 3 from the second row: cost ∣10−25∣=15 ;
In total, 3+3+10+15=31 .In the second test case, it's optimal to connect 1 from the first row with 1 from the second row, and 4 from the first row with 4 from the second row.