CF1765J.Hero to Zero
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There are no heroes in this problem. I guess we should have named it "To Zero".
You are given two arrays a and b , each of these arrays contains n non-negative integers.
Let c be a matrix of size n×n such that ci,j=∣ai−bj∣ for every i∈[1,n] and every j∈[1,n] .
Your goal is to transform the matrix c so that it becomes the zero matrix, i. e. a matrix where every element is exactly 0 . In order to do so, you may perform the following operations any number of times, in any order:
- choose an integer i , then decrease ci,j by 1 for every j∈[1,n] (i. e. decrease all elements in the i -th row by 1 ). In order to perform this operation, you pay 1 coin;
- choose an integer j , then decrease ci,j by 1 for every i∈[1,n] (i. e. decrease all elements in the j -th column by 1 ). In order to perform this operation, you pay 1 coin;
- choose two integers i and j , then decrease ci,j by 1 . In order to perform this operation, you pay 1 coin;
- choose an integer i , then increase ci,j by 1 for every j∈[1,n] (i. e. increase all elements in the i -th row by 1 ). When you perform this operation, you receive 1 coin;
- choose an integer j , then increase ci,j by 1 for every i∈[1,n] (i. e. increase all elements in the j -th column by 1 ). When you perform this operation, you receive 1 coin.
You have to calculate the minimum number of coins required to transform the matrix c into the zero matrix. Note that all elements of c should be equal to 0 simultaneously after the operations.
输入格式
The first line contains one integer n ( 2≤n≤2⋅105 ).
The second line contains n integers a1,a2,…,an ( 0≤ai≤108 ).
The third line contains n integers b1,b2,…,bn ( 0≤bj≤108 ).
输出格式
Print one integer — the minimum number of coins required to transform the matrix c into the zero matrix.
输入输出样例
输入#1
3 1 2 3 2 2 2
输出#1
2
输入#2
3 3 1 3 1 1 2
输出#2
5
输入#3
2 1 0 2 1
输出#3
2
输入#4
2 1 4 2 3
输出#4
4
输入#5
4 1 3 3 7 6 9 4 2
输出#5
29
说明/提示
In the first example, the matrix looks as follows:
111000111You can turn it into a zero matrix using 2 coins as follows:
- subtract 1 from the first row, paying 1 coin;
- subtract 1 from the third row, paying 1 coin.
In the second example, the matrix looks as follows:
221001221You can turn it into a zero matrix using 5 coins as follows:
- subtract 1 from the first row, paying 1 coin;
- subtract 1 from the third row, paying 1 coin;
- subtract 1 from the third row, paying 1 coin;
- subtract 1 from a2,3 , paying 1 coin;
- add 1 to the third column, receiving 1 coin;
- subtract 1 from the first row, paying 1 coin;
- subtract 1 from a2,3 , paying 1 coin.