B. Chips on the Board
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
You are given a board of size n × n n \times n n×n ( n n n rows and n n n colums) and two arrays of positive integers a a a and b b b of size n n n.
Your task is to place the chips on this board so that the following condition is satisfied for every cell ( i , j ) (i, j) (i,j):
- there exists at least one chip in the same column or in the same row as the cell ( i , j ) (i, j) (i,j). I. e. there exists a cell ( x , y ) (x, y) (x,y) such that there is a chip in that cell, and either x = i x = i x=i or y = j y = j y=j (or both).
The cost of putting a chip in the cell ( i , j ) (i, j) (i,j) is equal to a i + b j a_i + b_j ai+bj.
For example, for n = 3 n=3 n=3, a = [ 1 , 4 , 1 ] a=[1, 4, 1] a=[1,4,1] and b = [ 3 , 2 , 2 ] b=[3, 2, 2] b=[3,2,2]. One of the possible chip placements is as follows:
White squares are empty
The total cost of that placement is ( 1 + 3 ) + ( 1 + 2 ) + ( 1 + 2 ) = 10 (1+3) + (1+2) + (1+2) = 10 (1+3)+(1+2)+(1+2)=10.
Calculate the minimum possible total cost of putting chips according to the rules above.
Input
The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104) — the number of test cases.
The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 3 ⋅ 1 0 5 1 \le n \le 3 \cdot 10^5 1≤n≤3⋅105).
The second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,…,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1≤ai≤109).
The third line contains n n n integers b 1 , b 2 , … , b n b_1, b_2, \dots, b_n b1,b2,…,bn ( 1 ≤ b i ≤ 1 0 9 1 \le b_i \le 10^9 1≤bi≤109).
The sum of n n n over all test cases doesn’t exceed 3 ⋅ 1 0 5 3 \cdot 10^5 3⋅105.
Output
For each test case, print a single integer — the minimum possible total cost of putting chips according to the rules.
Example
inputCopy
4
3
1 4 1
3 2 2
1
4
5
2
4 5
2 3
5
5 2 4 5 3
3 4 2 1 5
outputCopy
10
9
13
24
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<string>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<numeric>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int>PII;
const int N=3e5+10;
const int MOD=998244353;
const int INF=0X3F3F3F3F;
const int dx[]={-1,1,0,0,-1,-1,+1,+1};
const int dy[]={0,0,-1,1,-1,+1,-1,+1};
const int M = 1e6 + 10;
ll t;
ll n, b[N], a[N];
int main()
{
cin >> t;
while(t --){
ll s1 = 0, s2 = 0;
cin >> n;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
}
for(int i = 1; i <= n; i ++)
{
cin >> b[i];
}
sort(a + 1, a + 1 + n), sort(b + 1, b + 1 + n);
for(int i = 1; i <= n; i ++)
{
s1 += a[i], s2 += b[i];
}
cout << min(n * b[1] + s1, s2 + n * a[1]) << endl;
}
return 0;
}