PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#GL0000142. Chào buổi sáng, ƯCLN (Ya Sabah El GCD)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Chào buổi sáng, ƯCLN (Ya Sabah El GCD)
Ya Sabah El GCD
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given two arrays and , both of length , with indices from to .
You may choose an index and replace with , paying a cost of . Repeating the same index has no further effect.
Find the minimum total cost needed to make . Print -1 if this is impossible.
Input
The first line contains ; the second contains ; the third contains .
Output
Print the minimum total cost, or -1 if achieving GCD is impossible.
Subtasks
- Subtask 1 (20 points): ; .
- Subtask 2 (30 points): ; .
- Subtask 3 (50 points): ; .
Examples
Input
3
6 30 60
5 10 21
Output
2
Explanation
Initially . Applying the operation at index costs and replaces with . The resulting array has GCD . Since every operation costs at least , the optimum is .