Official

A - 数値列の一致 / Matching Sequences Editorial by admin Editorial for printing

Qwen3-Coder-480B

Overview

Find the minimum number of operations required to make each element of numerical sequences \(A\) and \(B\) match. For each element, you can increment or decrement either of the two values by 1.

Analysis

For each element \(i\), making \(A_i\) and \(B_i\) equal requires \(|A_i - B_i|\) operations. This is because one of the values must be incremented or decremented until it matches the other.

For example, when \(A_i = 3\) and \(B_i = 7\), at least \(|3 - 7| = 4\) operations are needed. Since this operation does not affect other elements, each element can be considered independently.

Therefore, the sum of the number of operations across all elements gives the answer. Since it cannot be reduced further, this is the optimal solution.

Algorithm

For each element \(i\), compute \(|A_i - B_i|\) and take the total sum to obtain the answer.

Concrete steps: 1. For each \(i\), compute \(|A_i - B_i|\) and add it to the running total. 2. Output the final total.

Complexity

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(1)\) (excluding the input arrays)

Implementation Notes

  • Since we only need to sum the absolute differences of each element, we use a simple loop and the abs() function.
  • Although the input range is large (\(10^9\)), the sum of absolute differences can be up to approximately \(2 \times 10^{14}\), so in Python the standard integer type handles this without issue.
## Source Code

```python
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

total_operations = 0
for i in range(N):
    total_operations += abs(A[i] - B[i])

print(total_operations)

This editorial was generated by qwen3-coder-480b.

posted:
last update: