#GL0000374. Ước chung lớn nhất sau khi chỉnh sửa mảng (Array GCD)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Ước chung lớn nhất sau khi chỉnh sửa mảng (Array GCD)
Array GCD
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an array of integers . You may use each of the following operation types at most once:
- Remove one contiguous segment of length , where , at a cost of . Removing nothing () is allowed. You must not remove the whole array.
- Modify any subset of the remaining elements: each selected element may be increased by exactly or decreased by exactly , at a cost of for that element. Each element may be modified at most once. Modifying nothing is allowed.
Find the minimum total cost such that the GCD of the remaining array is greater than .
Removed indices must form a single contiguous segment, not several disjoint segments. The segment may be a prefix, an internal segment, or a suffix.
Input
The first line contains . The next line contains the integers .
Output
Print one 64-bit integer: the minimum total cost to make the remaining array GCD greater than .
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; ; .
Examples
Example 1
Input
3 1 4
4 2 3
Output
1
Explanation
Remove the last element for . The remaining array is with GCD , so the cost is . No element needs modification.
Example 2
Input
5 3 2
5 17 13 5 6
Output
8
Explanation
Remove the segment of length for coins; decrease to for more coins. The remaining array becomes , whose GCD is . Total cost is .
Example 3
Input
8 3 4
3 7 5 4 3 12 9 4
Output
13
Explanation
Delete positions through , the segment , at cost . The remaining array is . Decrease its last value from to , paying another . The final array has GCD , and the total cost is .