CF1765J.Hero to Zero

省选/NOI-

通过率:0%

时间限制:4.00s

内存限制:512MB

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 aa and bb, each of these arrays contains nn non-negative integers.

Let cc be a matrix of size n×nn \times n such that ci,j=∣ai−bj∣c_{i,j} = |a_i - b_j| for every i∈[1,n]i \in [1, n] and every j∈[1,n]j \in [1, n].

Your goal is to transform the matrix cc so that it becomes the zero matrix, i. e. a matrix where every element is exactly 00. In order to do so, you may perform the following operations any number of times, in any order:

  • choose an integer ii, then decrease ci,jc_{i,j} by 11 for every j∈[1,n]j \in [1, n] (i. e. decrease all elements in the ii-th row by 11). In order to perform this operation, you pay 11 coin;
  • choose an integer jj, then decrease ci,jc_{i,j} by 11 for every i∈[1,n]i \in [1, n] (i. e. decrease all elements in the jj-th column by 11). In order to perform this operation, you pay 11 coin;
  • choose two integers ii and jj, then decrease ci,jc_{i,j} by 11. In order to perform this operation, you pay 11 coin;
  • choose an integer ii, then increase ci,jc_{i,j} by 11 for every j∈[1,n]j \in [1, n] (i. e. increase all elements in the ii-th row by 11). When you perform this operation, you receive 11 coin;
  • choose an integer jj, then increase ci,jc_{i,j} by 11 for every i∈[1,n]i \in [1, n] (i. e. increase all elements in the jj-th column by 11). When you perform this operation, you receive 11 coin.

You have to calculate the minimum number of coins required to transform the matrix cc into the zero matrix. Note that all elements of cc should be equal to 00 simultaneously after the operations.

本题中没有英雄。我们本应将其命名为“归零”。

给定两个数组 aa 和 bb,每个数组均包含 nn 个非负整数。

定义一个 n×nn \times n 的矩阵 cc,其中对任意 i∈[1,n]i \in [1, n] 和任意 j∈[1,n]j \in [1, n],均有 ci,j=∣ai−bj∣c_{i,j} = |a_i - b_j|。

你的目标是将矩阵 cc 变为零矩阵(即所有元素均为 00 的矩阵)。为此,你可以以任意顺序、任意次数执行以下操作:

  • 选择一个整数 ii,将第 ii 行所有元素 ci,jc_{i,j}(j∈[1,n]j \in [1, n])减 11(即整行减 11)。执行该操作需花费 11 枚硬币;
  • 选择一个整数 jj,将第 jj 列所有元素 ci,jc_{i,j}(i∈[1,n]i \in [1, n])减 11(即整列减 11)。执行该操作需花费 11 枚硬币;
  • 选择两个整数 ii 和 jj,将元素 ci,jc_{i,j} 减 11。执行该操作需花费 11 枚硬币;
  • 选择一个整数 ii,将第 ii 行所有元素 ci,jc_{i,j}(j∈[1,n]j \in [1, n])加 11(即整行加 11)。执行该操作可获得 11 枚硬币;
  • 选择一个整数 jj,将第 jj 列所有元素 ci,jc_{i,j}(i∈[1,n]i \in [1, n])加 11(即整列加 11)。执行该操作可获得 11 枚硬币。

你需要计算将矩阵 cc 变为零矩阵所需的最少硬币数量。注意:所有操作完成后,矩阵 cc 的每个元素必须同时等于 00。

输入格式

The first line contains one integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5).

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (0≤ai≤1080 \le a_i \le 10^8).

The third line contains nn integers b1,b2,…,bnb_1, b_2, \dots, b_n (0≤bj≤1080 \le b_j \le 10^8).

第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(0≤ai≤1080 \le a_i \le 10^8)。

第三行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \dots, b_n(0≤bj≤1080 \le b_j \le 10^8)。

输出格式

Print one integer — the minimum number of coins required to transform the matrix cc into the zero matrix.

输出一个整数——将矩阵 cc 变为零矩阵所需的最少硬币数量。

输入输出样例

  • 输入#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:

1

1

1

0

0

0

1

1

1

You can turn it into a zero matrix using 22 coins as follows:

  • subtract 11 from the first row, paying 11 coin;
  • subtract 11 from the third row, paying 11 coin.

In the second example, the matrix looks as follows:

2

2

1

0

0

1

2

2

1

You can turn it into a zero matrix using 55 coins as follows:

  • subtract 11 from the first row, paying 11 coin;
  • subtract 11 from the third row, paying 11 coin;
  • subtract 11 from the third row, paying 11 coin;
  • subtract 11 from a2,3a_{2,3}, paying 11 coin;
  • add 11 to the third column, receiving 11 coin;
  • subtract 11 from the first row, paying 11 coin;
  • subtract 11 from a2,3a_{2,3}, paying 11 coin.

在第一个例子中,矩阵如下所示:

1

1

1

0

0

0

1

1

1

你可以花费 22 枚硬币将其变为零矩阵,具体操作如下:

  • 从第一行减去 11,花费 11 枚硬币;
  • 从第三行减去 11,花费 11 枚硬币。

在第二个例子中,矩阵如下所示:

2

2

1

0

0

1

2

2

1

你可以花费 55 枚硬币将其变为零矩阵,具体操作如下:

  • 从第一行减去 11,花费 11 枚硬币;
  • 从第三行减去 11,花费 11 枚硬币;
  • 从第三行减去 11,花费 11 枚硬币;
  • 从元素 a2,3a_{2,3} 减去 11,花费 11 枚硬币;
  • 向第三列加 11,获得 11 枚硬币;
  • 从第一行减去 11,花费 11 枚硬币;
  • 从元素 a2,3a_{2,3} 减去 11,花费 11 枚硬币。

输入解题思路,AI测评打分。不知道怎么写?

首页