PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#BS0000127. Biến đổi tất cả phần tử về cùng giá trị (±1 Operation 2)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Biến đổi tất cả phần tử về cùng giá trị (±1 Operation 2)
±1 Operation 2
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an integer array . One operation chooses a single index and increments or decrements its value by exactly . For each of independent queries , find the minimum number of operations required to make every element equal to . The original array is unchanged between queries.
Input
The first line contains . The second line contains values . Each of the following lines contains one target value .
Output
Print lines, each giving the minimum operations needed to make all entries equal to its target.
Subtasks
- Subtask 1 (20 points): ; for , .
- Subtask 2 (30 points): ; for , .
- Subtask 3 (50 points): ; for , .
Examples
Input
5 3
6 11 2 5 5
5
20
0
Output
10
71
29
Explanation
For , the distances are , summing to . For , the required increments are , totaling . For , the decrements sum to . Thus print 10, 71, and 29.