A14698 | Divan and Kostomuksha (easy version)
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
This is the easy version of the problem. The only difference is maximum value of $a_i$.
Once in Kostomuksha Divan found an array $a$ consisting of positive integers. Now he wants to reorder the elements of $a$ to maximize the value of the following function:
$$ \sum_{i=1}^n \operatorname{gcd}(a_1, \, a_2, \, \dots, \, a_i), $$
where $\operatorname{gcd}(x_1, x_2, \ldots, x_k)$ denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers $x_1, x_2, \ldots, x_k$ , and $\operatorname{gcd}(x) = x$ for any integer $x$.
Reordering elements of an array means changing the order of elements in the array arbitrary, or leaving the initial order.
Of course, Divan can solve this problem. However, he found it interesting, so he decided to share it with you.
Once in Kostomuksha Divan found an array $a$ consisting of positive integers. Now he wants to reorder the elements of $a$ to maximize the value of the following function:
$$ \sum_{i=1}^n \operatorname{gcd}(a_1, \, a_2, \, \dots, \, a_i), $$
where $\operatorname{gcd}(x_1, x_2, \ldots, x_k)$ denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers $x_1, x_2, \ldots, x_k$ , and $\operatorname{gcd}(x) = x$ for any integer $x$.
Reordering elements of an array means changing the order of elements in the array arbitrary, or leaving the initial order.
Of course, Divan can solve this problem. However, he found it interesting, so he decided to share it with you.
输入格式
The first line contains a single integer $n$ ( $1 \leq n \leq 10^5$ ) — the size of the array $a$ .
The second line contains $n$ integers $a_{1}, \, a_{2}, \, \dots, \, a_{n}$ ( $1 \le a_{i} \le 5 \cdot 10^6$ ) — the array $a$ .
The second line contains $n$ integers $a_{1}, \, a_{2}, \, \dots, \, a_{n}$ ( $1 \le a_{i} \le 5 \cdot 10^6$ ) — the array $a$ .
输出格式
Output the maximum value of the function that you can get by reordering elements of the array $a$ .
输入输出样例
输入 #1
6 2 3 1 2 6 2
输出 #1
14
输入 #2
10 5 7 10 3 1 10 100 3 42 54
输出 #2
131
In the first example, it's optimal to rearrange the elements of the given array in the following order: $[6, \, 2, \, 2, \, 2, \, 3, \, 1]$ :
$$ \operatorname{gcd}(a_1) + \operatorname{gcd}(a_1, \, a_2) + \operatorname{gcd}(a_1, \, a_2, \, a_3) + \operatorname{gcd}(a_1, \, a_2, \, a_3, \, a_4)\\ + \operatorname{gcd}(a_1, \, a_2, \, a_3, \, a_4, \, a_5) + \operatorname{gcd}(a_1, \, a_2, \, a_3, \, a_4, \, a_5, \, a_6)\\= 6 + 2 + 2 + 2 + 1 + 1 = 14. $$
It can be shown that it is impossible to get a better answer.
In the second example, it's optimal to rearrange the elements of a given array in the following order: $[100, \, 10, \, 10, \, 5, \, 1, \, 3, \, 3, \, 7, \, 42, \, 54]$.
$$ \operatorname{gcd}(a_1) + \operatorname{gcd}(a_1, \, a_2) + \operatorname{gcd}(a_1, \, a_2, \, a_3) + \operatorname{gcd}(a_1, \, a_2, \, a_3, \, a_4)\\ + \operatorname{gcd}(a_1, \, a_2, \, a_3, \, a_4, \, a_5) + \operatorname{gcd}(a_1, \, a_2, \, a_3, \, a_4, \, a_5, \, a_6)\\= 6 + 2 + 2 + 2 + 1 + 1 = 14. $$
It can be shown that it is impossible to get a better answer.
In the second example, it's optimal to rearrange the elements of a given array in the following order: $[100, \, 10, \, 10, \, 5, \, 1, \, 3, \, 3, \, 7, \, 42, \, 54]$.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted