PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#GL0000129. ƯCLN lớn nhất và tổng lớn nhất (Maximum Gcd and Sum)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
ƯCLN lớn nhất và tổng lớn nhất (Maximum Gcd and Sum)
Maximum Gcd and Sum
Source: HackerRank
Version: Phuoc Hung OJ Extended
Problem Statement
Given two arrays and , each of length , select from and from . First maximize among all possible pairs. Among pairs with this maximum GCD, maximize . Output the resulting sum.
Input
The first line contains . The second contains ; the third contains , each with numbers.
Output
Print the sum satisfying the two priority criteria.
Subtasks
- Subtask 1 (20 points): ; .
- Subtask 2 (30 points): ; .
- Subtask 3 (50 points): ; .
Examples
Input
3
6 10 15
9 12 20
Output
30
Explanation
The pair (10,20) has GCD 10 and sum 30. Every other cross-array pair has a smaller GCD, so the answer is 30.