PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#GL0000365. Tối đa hóa tổng GCD tiền tố – Bản khó (Divan and Kostomuksha (Hard))
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ối đa hóa tổng GCD tiền tố – Bản khó (Divan and Kostomuksha (Hard))
Divan and Kostomuksha (Hard)
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an array of positive integers. You may reorder every element arbitrarily. For any resulting order, define the -th prefix GCD by
Maximize .
Hard version: elements can be as large as , so visiting all pairs of multiples as in the easy version may be too slow. The intended optimization combines a prime sieve with divisibility DP.
Input
- Line 1: .
- Line 2: integers .
Output
Print the maximum prefix-GCD sum using a 64-bit integer type.
Subtasks
- Subtask 1 (20 points): .
- Subtask 2 (30 points): .
- Subtask 3 (50 points): .
Examples
Example 1
Input
6
2 3 1 2 6 2
Output
14
Explanation
Both versions optimize the same prefix-gcd sum. The order [6,2,2,2,3,1] gives 14.
Example 2
Input
10
5 7 10 3 1 10 100 3 42 54
Output
131
Explanation
The order [100,10,10,5,1,3,3,7,42,54] yields 131.