One day Sasha visited the farmer 2D and his famous magnetic farm. On this farm, the crop grows due to the influence of a special magnetic field. Maintaining of the magnetic field is provided by nn machines, and the power of the ii-th machine is a_iai.
This year 2D decided to cultivate a new culture, but what exactly he didn't say. For the successful growth of the new culture, it is necessary to slightly change the powers of the machines. 2D can at most once choose an arbitrary integer xx, then choose one machine and reduce the power of its machine by xx times, and at the same time increase the power of one another machine by xx times (powers of all the machines must stay positive integers). Note that he may not do that if he wants. More formally, 2D can choose two such indices ii and jj, and one integer xx such that xx is a divisor of a_iai, and change powers as following: a_i = \frac{a_i}{x}ai=xai, a_j = a_j \cdot xaj=aj⋅x
Sasha is very curious, that's why he wants to calculate the minimum total power the farmer can reach. There are too many machines, and Sasha can't cope with computations, help him!
Input
The first line contains one integer nn (2 \le n \le 5 \cdot 10^42≤n≤5⋅104) — the number of machines.
The second line contains nn integers a_1, a_2, \ldots, a_na1,a2,…,an (1 \le a_i \le 1001≤ai≤100) — the powers of the machines.
Output
Print one integer — minimum total power.
Sample 1
Inputcopy | Outputcopy |
---|---|
5 1 2 3 4 5 | 14 |
Sample 2
Inputcopy | Outputcopy |
---|---|
4 4 2 4 4 | 14 |
Sample 3
Inputcopy | Outputcopy |
---|---|
5 2 4 2 3 7 | 18 |
Note
In the first example, the farmer can reduce the power of the 44-th machine by 22 times, and increase the power of the 11-st machine by 22 times, then the powers will be: [2, 2, 3, 2, 5][2,2,3,2,5].
In the second example, the farmer can reduce the power of the 33-rd machine by 22 times, and increase the power of the 22-nd machine by 22 times. At the same time, the farmer can leave is be as it is and the total power won't change.
In the third example, it is optimal to leave it be as it is.
大意:有n个正整数,将其中一个整数乘以X,一个整数除以X得到新和,要求最小的和,并且变换后不能出现小数。
这道题主要需要保存一个数的约数,然后从大到小依次计算经过约数变化后的数和即可。
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
void calc(int a, int* b,int* i)
{
for (int j = a; j >= 1; j--)
{
if (a % j == 0) {
b[*i] = j;
(*i)++;
}
}
}
int main()
{
int n,minnum=0,p=0,sum=0;
cin >> n;
int* a = new int[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
minnum = minnum + a[i];
}
sum = minnum;
sort(a, a + n);
int* b = new int[100];//用于存储因数
for (int i = n - 1; i >= 1; i--)
{
p = 0;
memset(b, 0, 100);
calc(a[i], b,&p);//从最大开始计算因数保存到b内
for (int j = 0; j < p; j++)
{
int det1 = a[0] * b[j] - a[0];
int det2 = a[i] - a[i] / b[j];
if (sum+det1-det2<minnum)//变小
minnum = sum + det1 - det2;
}
}
cout << minnum;
}