PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#GL0000053. Tách số và cộng K (Split Plus K)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Tách số và cộng K (Split Plus K)
Split Plus K
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
There are n positive integers on the board and a positive constant k. In one operation, remove a value x and replace it with two positive integers y and z such that y+z=x+k. Determine the minimum number of operations needed until every number remaining on the board is equal. Print -1 if impossible. Zero operations are allowed.
Input
First line: n k. Second line: n positive integers a_i.
Output
The minimum number of operations, or -1 if impossible.
Subtasks
- Subtask 1 (20 points): , .
- Subtask 2 (30 points): , .
- Subtask 3 (50 points): , .
Examples
Input
2 1
3 4
Output
3
Explanation
Splitting 4 into 2 and 3, then splitting each 3 into two 2s, uses exactly three operations.